How to Make Your Own Django Cookiecutter Template!
Are you sick of going through the same repetitive steps everytime you kick off a new Django project? Well, the solution you’ve been craving is right here! 🤩
If you’re a Python back-end developer bursting with great ideas, then you most definitely feel like wasting your time while starting up a new project. But today’s the day you’re gonna get rid of all those pointless steps, thanks to the game-changing power of Django cookiecutter.
What is the cookiecutter project?
In simple terms, Django cookiecutter is a template for jumpstarting Django projects in just a few seconds. It was originally created and led by Audrey Roy Greenfeld. Right now, you can easily make use of Django cookiecutter by installing cookiecutter:
$ pip install "cookiecutter>=1.7.0"
And then running this command:
$ cookiecutter https://github.com/cookiecutter/cookiecutter-django
You’ll be asked some questions, after providing the answers your project will be ready.
Why make a custom cookiecutter template?
Using the magic of the cookiecutter project is all fun and games, but what if you’re not comfortable with the project structure? What if you want some other standards implemented in your code? How can you customize all the files and directories?
This is the moment where you might start thinking of making your very own Django cookiecutter template. It’s super-duper easy, thanks to the availability of the project source-code and it’s great documentation. So let’s not waste anymore time and get right to it.🚀
Install cookiecutter
Obviously, the first step is to install the cookiecutter package:
$ pip install "cookiecutter>=1.7.0"
Start a Project
Start up a Django project as you normaly do:
pip install django
django-admin start project myproject
Your project with look like this:
myproject
┣ myproject
┃ ┣ __init__.py
┃ ┣ asgi.py
┃ ┣ settings.py
┃ ┣ urls.py
┃ ┗ wsgi.py
┗ manage.py
Create the cookiecutter.json
file
Now let’s start turning our project into a cookiecutter template.
Go on and make a cookiecutter config file named cookiecutter.json
along the main project folder. This file is responsible for asking the initial questions and getting the answers. So just think of the regular stuff you might use in your everyday projects, and turn them into short-answer questions. Your cookiecutter.json
file should look something like this:
{
"project_name": "My Django Project",
"project_slug": "{{ cookiecutter.project_name.lower()|replace(' ', '_')|replace('-', '_')|replace('.', '_')|trim() }}",
"description": "Behold My Awesome Django Project!",
"author_name": "Your name",
"use_drf": "y",
"use_simple_jwt": "y"
}
- I mentioned only a couple of options you might want in your project. This file is where you can have anything you want.
Use the cookiecutter options
Now it’s time to make use of the answers the user provides for the questions. The first step is to change the project name. So just look for the folders with the name myproject
and replace every result with {{ cookiecutter.project_slug }}
.
Also don’t forget to change all the myproject
occurrences too. If you’re using vscode
, you can do so by pressing Ctrl + Shift + H
:
Use conditions for cookiecutter template
There might be several lines of codes that are conditional to some of the answers provided by the user. To sort these kinds of situations out, you can use {% if condition %} — {% endif %}
. For example, your requirements.txt will look like this:
# Django
Django==4.2.2
# Django-filters
django-filter==23.2
# Decouple
python-decouple==3.8
# Pillow
Pillow==9.5.0
{% if cookiecutter.use_drf == 'y' %}
# Django Rest Framework
djangorestframework==3.14.0
django-cors-headers==4.1.0
{% endif %}
{% if cookiecutter.use_simple_jwt == 'y' %}
# JWT
djangorestframework-simplejwt==5.2.2
{% endif %}
- You might want to make files conditionally too. For example, you may want to make
Dockerfile
anddocker-compose.yml
only when user says yes touse_docker
; in this case, you can name your files like this:
{% if cookiecutter.use_docker == 'y' %}docker-compose.yml{% endif %}
Final step
When you’re done with customizing your template, you can push it to your git repository and run the bellow command:
$ cookiecutter https://github.com/your-django-cookiecutter-template
Just go ahead and answer the questions, and voila! You’ll have your django project with all the basic setups ready to be developed into something amazing 🥳
Final Horizons
Django cookiecutter is a fantastic project, but it might not be the best for you or your company’s needs. In this case, you can easily make your own cookiecutter template by following a few quick steps.
If you make, or have made your own cookiecutter template, I would love to take a look at it, so don’t hesitate to add your repository link in the comments 😍. You can also find my custom tempelate here.