Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Custom app template to use in Django's `startapp` command

License

Notifications You must be signed in to change notification settings

GriceTurrble/django-app-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Django app template

Custom app template to use in Django's startapp command.

What is this?

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.

What's included

New modules

  • 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 the urlpatterns list, as well as app_name.
  • signals.py: Often when folks get started with signals, they will tuck them into models.py, which is discouraged. I opt to standardize the module where these signals live.
    • As noted below, the app's AppConfig is also modified to include the import for this signals module by default, which makes wiring up the signal receivers easier.
    • If signals.py is removed from the app after the fact, its import should also be removed from the AppConfig's ready method.

Changes

  • 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 ready method, which for now simply imports the signals.py module to allow its receiver functions to be registered on app startup.
  • models.py:
  • views.py:
    • Coming soon Some best-practice examples for using Class-Based Views will be embedded into this module template in a future release.

Usage

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.

Option 1: use the asset URL

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

Option 2: download and use app_template.zip locally

  1. Download the latest app_template.zip and save it wherever you like.
  2. Use the path to this archive in the --template argument for the startapp management command:
$ python manage.py startapp --template path/to/app_template.zip myapp

You 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.

Release contents

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.

About

Custom app template to use in Django's `startapp` command

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published