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