Deployment is the final stage of website development. After testing, you deploy your project, making your website accessible to everyone via the specified URL. Until now, you have been using Django's internal webserver to run the projects locally on your computer. You can't share the localhost address, so nobody else can use the application. Therefore, to make your site available for all to use, you need to deploy the project code to an external server.
Various server providers exist, but this topic will focus on Heroku. Heroku is a popular hosting service that is free for small projects. The deployment process on Heroku is rather straightforward. Other possible options for code deployment include GitHub or PythonAnywhere.
Heroku setup
Before using Heroku's services, you need to create a Heroku account and its Command Line Interface (CLI). This allows you to deploy your site from the terminal directly. To create a Heroku account, visit the Heroku website.
If you already have an account, click on Log In. If not, select Sign Up to create an account. This action will take you to the sign-up form:
Fill in your credentials and verify your email address.
Next, download and install the Heroku CLI. The installation instructions will vary based on your operating system:
For Ubuntu 20+:
$ sudo snap install --classic herokuFor Mac:
$ brew tap heroku/brew && brew install herokuFor Windows:
Since Windows does not have a package manager, you need to download and configure the CLI manually. Find the detailed installation instructions on the Heroku website.
Verify your installation by running:
$ heroku --versionIf the terminal displays the software's version without any errors, then the installation is successful! Now, you need to log in to your Heroku account. To do this, enter the following command:
$ heroku loginYour default browser will open a login page. Enter your Heroku credentials to log in.
Project setup
After logging into Heroku, you need to configure some of your code's files and settings. To start, activate your project's virtual environment for smooth deployment.
Heroku will automatically identify your app as a Python app if any of the following files exist in its root directory:
requirements.txt
setup.py
Pipfile
If none of these files is in your app's root directory, the Python buildpack will fail to identify your application correctly. Therefore, make sure you have at least one of these files.
To deploy a project on Heroku successfully, complete the following steps:
Create a new Procfile file
Install
gunicornCreate a Heroku app
Modify settings.py
Update requirements.txt
Initialize git and commit
Deploy on Heroku
Now, let's discuss these steps. First, create a Procfile. A Procfile is a text file that provides startup commands for your app. Although it's a text file, it shouldn't have an extension. So, for example, Procfile.txt won't work. Learn more about Procfiles in the Heroku documentation. Create a Procfile by going to your project directory in your terminal and typing:
$ touch ProcfileOpen the Procfile in a text editor and write: web: gunicorn <projectname>.wsgi. Replace <projectname> with your project's name.
The next step involves adding 'gunicorn' to your project. Gunicorn is a Python WSGI HTTP Server. WSGI (Web Server Gateway Interface) is a communication standard between the web server and your application. As a scalable and reliable library, Gunicorn includes a WSGI server, which allows you to run any web applications supporting WSGI (including both Flask and Django applications). Install it using the following command:
$ pip install gunicornOnce you have successfully installed gunicorn, you are ready to create a Heroku app using the Heroku CLI.
In the terminal, type:
$ heroku createThis command will create a Heroku app for you. Heroku also provides a domain, which the terminal will display as the output of the heroku create command. You'll need to add this domain to your settings.py.
Now, modify a couple lines and add another one in settings.py. Go to settings.py and make the following changes:
ALLOWED_HOSTS = ["<domain provided by 'heroku create' command>",]
DEBUG = FalseAdd the following line at the end of the file:
STATIC_ROOT = BASE_DIR/'staticfiles'In this case, you have set DEBUG = False and created STATIC_ROOT at the same time. However, it's better to create the static root before setting DEBUG = False. When you create the static root, you're designating a location for your project to find and execute all the HTML and CSS it uses. After running python manage.py collectstatic, Django will search for all the system's static files and move them to this location. Your static file server should map to this folder, wherever it is located. After running collectstatic, check the static root directory to see the directory structure Django has built.
Even after serving the static files, your project may still need additional work before deployment. If DEBUG = True, debugging becomes easier since errors are displayed clearly. Therefore, it's good practice to set DEBUG = False after completing all processes and right before deployment.
Almost done with the setup! You now need to create (or update) your requirements.txt file. Go to your project terminal and type: pip freeze.
This command displays a list of all packages that your app uses. For Windows users, copy all listed packages and paste them into the requirements.txt file in your project directory. For Linux, Mac, or Bas users, enter pip freeze > requirements.txt to create a requirements file.
Now your project setup is complete and ready for deployment.
Deployment
To deploy, first, initialize a repository, add your codes, and commit them. Type:
$ git init
$ git add -A
$ git commit -m "Ready to Deploy"Then, deploy your codes using the following command:
$ git push heroku masterOnce the code has been pushed and Heroku finishes installing the requirements, you can access your domain like this:
$ heroku openIf that doesn't work, copy the earlier provided domain and paste it into your browser. The deployed project should appear.
Conclusion
Congrats! You've deployed your Django Project! This topic has explained each stage of the process in detail. Heroku is convenient for deployment because it hosts easy-to-use deployment features and provides clear error messages for deployment issues. You can also set environment variables and databases on your production site.
Let's reinforce what you've learned by answering some questions and completing a few tasks!