© Ben Lopatin 2020
B. LopatinDjango Standalone Appshttps://doi.org/10.1007/978-1-4842-5632-9_24

24. Using app templates

Ben Lopatin1 
(1)
New York, NY, USA
 

Once you’ve got the hang of creating your own standalone apps, you might want to start writing more. At this point you’ll likely find there are a number of decisions you don’t want to make over and over again and necessary boilerplate that you don’t want to deal with writing. One solution to this is to start creating apps from templates.

In this chapter, we’ll review some of the options for creating new standalone apps from templates, including Django’s own startapp management command for starting new apps and the venerable Cookiecutter tool.

startapp

You’ve probably read about and used Django’s startapp management command to create new apps within your own Django projects:
./manage.py startapp myapp
By default this command will take an app name and create a directory with minimal file structure, including models.py and tests.py files, based on a templates directory structure in the Django package:
myapp/
        migrations/
        __init__.py
        admin.py
        app.py
        models.py
        tests.py
        views.py
The core functionality of the command is described for creating apps within the context of an existing project. However, there is no reason the command cannot be used to create the app structure anywhere else; simply use the django-admin script instead:
django-admin startapp myapp
By itself this isn’t terribly useful, as the only benefit is a small collection of all-but-blank files created in a specific directory. This can be improved by creating and using your own template directory and providing this an argument to the startapp command using the --template flag, for example:
django-admin.py startapp myapp --template ~/app.template

By using your own template, you can not only choose different files to use, you can pre-populate them with common imports and other code you use. The startapp command has support for a few specific context variables, including the app name, so you can also include some app-specific references in these files. Moreover, you can change the entire structure, including the app as a package within a parent directory with your setup files, README, among others.

This strategy will likely suffice if you’re creating apps based on the exact same structure and feature set each time. However, the minimally supported template context means that even with templating support, there’s little room for configurability.

Cookiecutter

For a more robust alternative, consider using Cookiecutter. Cookiecutter is a Python package described as “A command-line utility that creates projects from cookiecutters (project templates).” Using a combination of Jinja templates and a cookiecutter JSON configuration file, you can create highly configurable projects based on a single cookiecutter from an interactive prompt.

After installation using pip, brew on macOS, or apt-get on Debian, creating a project from a remote template is a single command:
cookiecutter https://location.com/of-the-cookiecutter.git
  • It’s worth noting that while Cookiecutter is a Python package and is widely used in the Python community, cookiecutter project templates can be created and used for any kind of project, irrespective of the language.

Perhaps more significant than the feature set provided by Cookiecutter is the community of publicly shared cookiecutter project templates, including templates for creating both “vanilla” Python packages and Django standalone apps. The benefit of using a community-built template is several fold, including eliminating the time and sundry decisions required to create a template from scratch, as well as getting a number of best practices “for free” which have been vetted by numerous users.

The primary downside is less that a template might exclude some feature that you want and more that they might be overly complex and feature rich for your needs. It may be more effort to edit than to start from scratch. In the case of the most popular Django package cookiecutter, pydanny/cookiecutter-djangopackage, there aren’t many overly specific decisions included, meaning it’s a safe starting point that won’t add unneeded cruft. It will enforce a top-level package (as opposed to using a source directory) and the specific Python and Django versions may not be quite up to date. Thankfully you can change these things. Figure 24-1 provides an example of the prompt provided by pydanny/cookiecutter-djangopackage for configuring a new Django standalone app.
../images/486718_1_En_24_Chapter/486718_1_En_24_Fig1_HTML.jpg
Figure 24-1

Prompt provided by pydanny/cookiecutter-djangopackage

There are two ways to create your own cookiecutter project template for starting Django standalone apps: from scratch or by adapting an existing project template. Adapting an existing cookiecutter is as simple as cloning the source repository, making the requisite changes, and using your local clone as the template.
cookiecutter path/to/local/cookiecutter

Adapting an existing template means you don’t have to start everything from scratch, from figuring out templated names for files to deciding how to track various package dependencies. If the changes you need are too radical, you can always start from scratch. When starting from scratch, keep in mind that despite the vast value of templating, the core of a template is copying files from one source to another. In other words, create your structure with as little configuration as possible to start and build up the configurability only as you need.

Summary

In this chapter, we looked at two ways of creating new Django standalone apps from templates: using Django’s startapp command and the general-purpose project templating tool Cookiecutter. Both can be used with custom starting templates to enforce project design decisions for subsequent Django standalone apps; however, Cookiecutter’s templating is more flexible than startapp and should be given first consideration as a standalone app templating solution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.244.201