Deploying Django project to pythonanywhere

Deploying Django project to pythonanywhere
Author: Mohammad Amaan Abbasi | May 18, 2019

Uploading Django to pythonanywhere

Step 1: Upload project to github

You can directly upload the files to pythonanywhere but cloning your project from github is recommended.

Before uploading your file you also need to add one thing in the ALLOWED_HOST list inside file, that is your domain:


Otherwise, you will get INVALID HTTP_HOST_ERROR.

Step 2 : Clone the repository

Open up bash console in pythonanywhere and clone you code

git clone yourprojectlink

Step 3 Create and Activate virtualenv

virtualenv --python=python3.7 venv

source venv/bin/activate

Step 4 Install Django and other requirements neccessary

If your webapplication is only based on Django, then run:

pip install django

Or, if you have requirements.txt file, run:

pip install -r requirements.txt

Step 5 Settings Up DataBase (optional)

follow this step only if your application uses a database, Otherwise go for step 7.

Default database settings provided by Django:

    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

You need to create a databse in pythonanywhere under the database section and set a password for it. After you have done that you your settings

#Production DB settings
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'username$dbname',
        'USER': 'username',
        'PASSWORD': 'dbpassword',
        'HOST': '',

Step 6 Make migrations

Head back to the console and run migrations to populate the database setings, make sure you are in the directory containing the file.

$ python migrate

Step 7 Create Webapp

Under the Web section click on add new web app.

Click Next.

Then Select manual configuration then click Next.

Then choose the python version, and click Next.

Step 8 Configure Webapp

After it finishes you will see a page containing a bunch of information. Right now our project is not linked to this webapp.

We will start by telling the webapp the path to the virtualenv. In the virtualenv input put the name of the virtualenv that we created before.


Step 8.1 Configuring wsgi file.

Open the wsgi configuration file

Scroll Down until you see, configuration related to Django.

# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys

# assuming your django settings file is at '/home/username/mysite'
# and your is is at '/home/amaanx/mysite/'
path = '/home/username/projectname' <------ a change
if path not in sys.path:

os.environ['DJANGO_SETTINGS_MODULE'] = 'projectname.settings' <----- b change

# then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

You can keep this part of the code and delete the other.

There you need to update two things:

a. The path to the project b. The name of the module that contains the file.

If you have wierd directory structure, i.e. you have the settings file deep inside the structure you would make changes to b accordingly




Save changes and Reload the app. Open up the link and probably you webapplication will now be working.

If it doesn't, you can check the errors at the error.log link. you can do a quick google search to solve the errors or you can post your error in the comments below, I will help you to solve them.

Bonus tips

Security tip

Set DEBUG=False in the file, Once your application is live. As it can reveal the internals of your application and hackers can find way to break into your application.

Css files

It is likely that you might not be able to see your fancy css and static files like imags in your application after you deployed it. It is because Static files are served differently by different servers.

When you work on your site locally, Django itself handles the serving of static files.

But once you have uploaded your file to pythonanywhere you need to do a few things.


Add this line at the end,

    os.path.join(BASE_DIR, "static"),

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")

in, add:

from django.conf import settings


if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Then once you have done that push the changes.

And in pythonanywhere console, run the following command:

python collectstatic

This will collect all the static files and put it in a directory static_cdn.

Now you need to pythonanywhere about this directory, head over to the web section. Scroll down until you see Static Files section.

In URL put : /static/ In path put: /static_cdn/

Now reload the Application, If you can't see the changes try to clear the cache of your browser and reload again.