Skip to content
Home » Django Portfolio Project – Part 6 – Deployment

Django Portfolio Project – Part 6 – Deployment

Spread the love

Time to share our app with the world. In this final part of the Django series, we’ll be talking about deployment. Not just talking, actually, we’ll deploy our application to production.

There are a couple deployment options as far as Django applications are concerned. Some of them have free tiers with time or functional limitations. We’re going to use PythonAnywhere. It takes care of the infrastructure (servers, load balancers, reverse proxies, etc.), which is great help when we only get started.

The limitations change from time to time, so you better check them out before you deploy your app. At the time of this writing, for example, there is no Postgres support in the free plan, so we’re going to stick with the default SQLite database, which is fine for our purposes.

But before we publish our website, we must get it ready to publish.

Before Publishing

So far, we’ve been developing our website using some settings that were meant to facilitate this process. But for production, we need some other settings. Let’s make all the necessary changes then.

SECRET_KEY and DEBUG

The two settings that definitely require some changes are SECRET_KEY and DEBUG. We’ll get them from environment variables if they are defined, and if not, we’ll use the values from the .env file in the root or the default values in the configuration file.

In order to read environment variables from a file, we’ll install the python-dotenv library:

Next, in the /portfolio/settings.py file, right below BASE_DIR, add the following code to support environment variables from the .env file:

We have to disable the original SECRET_KEY and add the new one:

We also have to change the DEBUG setting below:

HTTP Server and Database

Next, let’s install gunicorn, which is a Python HTTP server:

As far as database configuration is concerned, we’ll install dj-database-url to extract the database configuration from an environment variable:

Now we need to modify the database configuration in the /portfolio/settings.py file. Add the following code at the end of the file:

This configuration will be used if the environment variable is set. Otherwise, it will use the default SQLite database.

In case a Postgres database should be used, Django needs psycopg2, so let’s install it too:

Static Files

We must handle serving static files in production. We use the collectstatic tool to copy the static files to the location specified in the STATIC_ROOT path. Make sure your settings match the following:

We’ll use the WhiteNoise library to actually serve the files, so let’s install it:

We have to add it to the MIDDLEWARE list in settings.py:

Requirements

For the hosting services to know which dependencies to install, we have to add a requirements.txt file to the root. In the repo root, let’s run the following command:

The file contains all the dependencies that need to be installed:

We’re ready to publish the website now.

PythonAnywhere Account

Without further ado, let’s create a PythonAnywhere account.

Go to https://www.pythonanywhere.com/pricing/ and hit the Create a Beginner account button:

Create a Beginner account button

Fill in the form and hit Register:

Register

You’ll be sent a confirmation email:

confirmation email

Go check your inbox and confirm your email.

Next, optionally, you can take a quick tour to learn how the site works:

quick tour

After that, you’ll be logged in to the dashboard:

dashboard

Source Code

Now, we can fetch our source code from Github. But first, we have set up a virtual environment. There are a couple steps to take.

Click on Consoles to open the Console management screen:

Consoles

Click on Bash to create a new console:

Bash

The console will launch automatically. Type in the following command to create the virtual environment:

Here’s what it looks like in the console:

mkvirtualenv

In the next step, you have to clone the repo from Github. You must name the new folder after your PythonAnywhere username. For example, if my username is prosperocoder, the new folder must be named prosperocoder.pythonanywhere.com. Here’s how I’m cloning the repo in the console:

Navigate to the new folder:

Here it is in the console:

git clone

Next, let’s install the dependencies using the requirements.txt file:

This will install all the required libraries in the virtual environment on the host.

Next, let’s configure an SQLite database on the host:

Let’s collect the static files:

To access the site, we have to create a superuser:

Take note of the username, email address and password that you enter. You’re going to need them.

With the sources on the host, we must set up the web application now.

Web App Setup

If you are still in the console, expand the menu in the top right corner and navigate back to the dashboard:

back to dashboard

Go to the Web section and hit the Add a new web app button:

Web section

In the free tier you can’t do much about your domain name:

domain name

So, just click Next. In the next screen, select the Manual configuration option:

Manual configuration

In the Select a Python version screen, select Python 3.10:

Python version

In the next screen, hit the Next button:

configuration

All done. Your application has been created. You can use the green Reload button after you make any further changes. Also, as stated below, the app will run for three months. After this period of time, you have to click the yellow Run until 3 months from today button if you want to use it longer:

Run until 3 months from today button

Scroll down to the Code section. Click the link to the WSGI configuration file:

link to the WSGI configuration file

Replace the code in the file with this (naturally, use your own username instead of prosperocoder):

Save the file:

Save the file

Go back to the dashboard and in the Web section scroll down to the Virtualenv section:

Virtualenv section

Click the link Enter path to a virtualenv, if desired and enter the path to the virtual environment we created:

/home/prosperocoder/.virtualenvs/env_portfolio

Naturally, replace your username and the name of the virtual environment if it’s different. Now you should see it:

Virtualenv

Next, scroll down to the Static files section:

Static files section

Click Enter URL and enter the location where the static files were copied (\static_files\):

static files

Scroll up to the top of the Web section and hit the Reload button. Next, click the link to the page:

Reload button

You should see a disallowed host error.

Go to your Github project and add the host to ALLOWED_HOSTS in the settings.py file:

The app uses CSRF protection, so add this line right below:

Push the changes to Github. Next, go to the Bash console on PythonAnywhere that you used before and pull the changes there:

The last thing we have to take care of is creating the .env file from which DJANGO_DEBUG, DJANGO_SECRET_KEY and DATABASE_URL will be read.

In the Bash console, you can set the values in the .env file using the echo command. To set DJANGO_DEBUG, type the following:

Go back to the dashboard and in the Web section, reload the application and click the link again. Now you should navigate to the website:

live

This is your live website. The only problem is, you don’t see the stuff you entered through the admin site to the database. This is because we’re using a different SQLite database in production and it’s empty. We could enter all the data again through the admin site, or, which seems preferable, we can copy the database file.

Database Copy

In order to copy the database file, we have to find it first. Here we can see it in Visual Studio Code:

database file in VSC

So, it’s inside the portfolio folder. Here it is in the File Explorer:

database file in File Explorer

We have to upload this file to PythonAnywhere.

To do that, go to the Files section (A) and navigate to your project folder (B). You should now see the new db.sqlite3 file (C). This is the one with the empty database, so let’s delete it (D):

file upload

Hit the Upload a file button (A) and upload the file (B):

file upload

If you now go to the Web section, reload the application and click the link, all the data from the database should be there:

the website live

We can now use the admin site to manage our stuff. We can add projects, categories, etc. We can edit them or delete them if we want. Our website is live!

Conclusion

In this final part of the Django series we deployed our little app to production. But there’s much more to Django than this. We haven’t touched on topics like sessions, authentication and authorization or forms. We haven’t discussed testing Django applications. But for now, it will do. There’s much more to explore in the future.


Spread the love

Leave a Reply