Custom app template to use in Django's startapp command.
By default, Django's startapp management command will create a new app by using their built-in app template. Files and sub-directories are copied, with files being passed through Django's template language system in order to generate app-specific details.
These templates are barebones and, for my tastes, need a fair amount of work to get to a starting point where they're actually usable.
Thankfully, the startapp command takes an optional --template argument, which can accept either a directory with template files or an archive containing those files. The contents of the app_template/ directory you see in this repo are an example of those template files.
urls.py: Though not required for an app to function, I tend to build apps into a site that include their own URL patterns, so this is a standard addition for me. Includes the basic components to start adding paths to theurlpatternslist, as well asapp_name.signals.py: Often when folks get started with signals, they will tuck them intomodels.py, which is discouraged. I opt to standardize the module where these signals live.- As noted below, the app's
AppConfigis also modified to include the import for this signals module by default, which makes wiring up the signal receivers easier. - If
signals.pyis removed from the app after the fact, its import should also be removed from the AppConfig'sreadymethod.
- As noted below, the app's
- All modules:
- Modules include examples (commented out) for standard objects that might be included and links to interesting documentation.
- Modules, classes, and functions include basic PEP 257-compliant docstrings
- In some projects, I use a pre-commit hook that checks for this. The templated docstrings should make any new app pass such a test.
- Files have been formatted with Black ahead of time: running Black on the newly-created files should yield no changes.
apps.py:- Adds the
readymethod, which for now simply imports thesignals.pymodule to allow its receiver functions to be registered on app startup.
- Adds the
models.py:- A warning states not to use Django's built-in
Usermodel directly, but to calldjango.contrib.auth.get_user_model, instead. get_user_modelis also called at the top level of the module to obtain theUserclass as an example.
- A warning states not to use Django's built-in
views.py:- Coming soon Some best-practice examples for using Class-Based Views will be embedded into this module template in a future release.
See Django docs for the startapp command for details. You will need to use the --template argument to pass the new template in place of Django's default.
Copy the below command to use the ZIP archive direct from the Releases on this repo:
$ python manage.py startapp --template https://github.com/GriceTurrble/django-app-template/releases/download/v0.2.0/app_template.zip myapp- Download the latest
app_template.zipand save it wherever you like. - Use the path to this archive in the
--templateargument for thestartappmanagement command:
$ python manage.py startapp --template path/to/app_template.zip myappYou can also un-ZIP the archive to a directory within your project, then point to that directory:
$ python manage.py startapp --template path/to/app_template_dir/Doing so will also allow you to customize the template to your own project's needs, useful if you need to add lots of apps to the same site.
While the Django docs specifically mention using a GitHub archive ZIP as an option (choosing the "Download repo as ZIP" option), doing so will also copy this README, the LICENSE, and other files that you don't want mucking up your project space.
Instead, Releases on this repo include an app_template.zip, which contains only the file contents of the app_template/ directory. This provides a clean starting point for your new app.