5 Tips For A Solid Django Development – Deployment Flow

You’ve now finished your prototype and now you are ready to launch your passion project. You’ve previously finished one or two Django projects before but this is the first time that you are going to deploy the app and put it in front of an audience.

You might be asking, What’s a solid development and deployment process to follow? Should I learn docker first? Do I need to practice git and learn how to create branches, rollbacks? How do I version my project?

In this post, we’ll discuss the things that you need to have in place for a smooth development – deployment workflow. I am assuming you are deploying to a Linux machines, which you should do 😉

1. Make sure that you are using a dependency manager like Pipenv

You are using Pipenv because you want your virtual environment created for you and you want the Python dependencies installed in that virtual environment. Pipenv allows you to manage both in one command.

You can now deploy your application by:

  1. Zipping the whole project
  2. Extracting it to the server
  3. Edit .bashrc, the line PIPENV_VENV_IN_PROJECT=1 , this will make pipenv install the virtualenv in $YOUR_PROJECT_DIR/.venv
  4. Install the dependencies via pipenv install
  5. Run your Django application by executing: pipenv run gunicorn module:app

2. Check settings.py and make sure you are not hard coding sensitive information

A good example of this is the value of SECRET_KEY. It’s good practice to use os.getenv():

SECRET_KEY = os.getenv('SECRET_KEY', 'x^7xp)w8u9i94iwri5(*!h@%m1-ik5z45stz&3(@-t%+ml8am#')

This gets the value of the environment variable SECRET_KEY. It falls back to the value of the second parameter if it is not found.

An advantage of this is the sensitive values of your applications such as credentials are not saved in Git. It also prepares your application better if you want to use Docker in the future.

3. Use Git. You can use branching but if you’re a sole developer, it can be optional

If you’re a sole developer working on a project, you can opt not to apply branching strategies but we’d suggest that you do. If you’re adding more team members to the project or if you’re going to work in a company, it will be required.

Also, knowing the difference between git reset Vs revert will be useful if you want to rollback changes.

4. Use Nginx and Gunicorn

You might be asking, why do I need both? Gunicorn, serves your Django app. It translates the requests coming from Nginx into a format that your web app understands. Nginx is recommended because it can efficiently handle incoming connections. It is also really good at serving static files. Using Nginx in this way is also considered as secure because it buffers slow clients which can be used as a denial-of-service attack.

5. Use systemd to easily start your web app and to always keep it running!

systemd runs your web app as a Linux service. Using it allows your web app to run in the background and it automatically starts your service if the machine restarts.

Following these tips will make your web application ready to accept traffic in production.

In the next posts, we’ll discuss how you can use Docker to package everything and make your development and deployment process even smoother so please make sure to sign up to our mailing list below.

Do you want more Python and Django tips and tutorials? Drop your email in the box below and we'll send new content straight to your inbox.

Leave a Comment