Mr. Editor-in-chief Mr. Editor-in-chief October 10, 2020 Updated April 24, 2026

Django & PythonAnywhere

Deploy a Django application onto PythonAnywhere platform
1 Create a new repository on GitHub
2 Prepare and Push the code base onto GitHub

pip freeze > requirements.txt
git add -A
git commit -m "ready to deploy"
git remote add origin git@github.com:username/repo_name.git
git push -u origin main

3 Pull the code from GitHub to PythonAnywhere virtual machine Assuming that you have registered a PythonAnywhere account. Go to the Dashboard and click on $Bash
Create a pair of keys in PythonAnywhere VM and add the public key into GitHub. Refer to SSH Academy

ssh-keygen -t ecdsa -b 521
cd .ssh
cat id_ecdsa.pub

copy the content in id_ecdsa.pub and go to www.github.com -> GitHub profile picture (on the top right) -> settings -> SSH and GPG keys -> SSH keys / Add new -> paste it into 'Key'

cd ..
git clone git@github.com:username/repo_name.git

4 Create a virtual environment and install Django and other dependencies

cd [project_name]
mkvirtualenv --python=/usr/bin/python3.10 "[venv name]"
pip install -r requirements.txt

5 Setting up web app and WSGI file
Find 3 pieces of information and better note them down for later usage
I The path to Django project's top folder -- the folder that contains "manage.py"

(venv) 09:35 ~/markdown_blog (main)$ ls
blog  db.sqlite3  manage.py  posts  requirements.txt  templates
(venv) 09:34 ~/markdown_blog (main)$ pwd
/home/pengworks/markdown_blog # Information I

II The name of project -- the name of the folder that contains your settings.py

(venv) 09:36 ~/markdown_blog (main)$ cd blog
(venv) 09:37 ~/markdown_blog/blog (main)$ ls
__init__.py  asgi.py  settings.py  urls.py  wsgi.py
# Information II: blog

III The name of virtual environment(venv)
It's venv for Information III in this case.
Go to Web --> Add a new web app --> Next --> Manual configuration
- Virtualenv: Information III
- Source code: Information I
- Working directory: Information I
- WSGI configuration file

/var/www/pengworks_pythonanywhere_com_wsgi.py

import os
import sys
# assuming your django settings file is at '/home/pengworks/markdown_blog/blog/settings.py'
# and your manage.py is is at '/home/pengworks/markdown_blog/manage.py'
path = '/home/pengworks/markdown_blog' # Information I
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'blog.settings' # [Information II].settings

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

6 Create a dotenv file for production settings

cd ..
touch .env
nano .env  # Ctrl + O, Enter, Ctrl + X to save and exit

sample .env file content for production

DJANGO_SECRET_KEY='whatever_truly_secret_key'
DEBUG=False
ALLOWED_HOSTS=.localhost,127.0.0.1,pengworks.pythonanywhere.com

7 Setting up static and media files Go to Web --> Static files:
- Enter URL: /static/
- Enter path: [Information I]/static
- Enter URL: /media/
- Enter path: [Information I]/media

8 Migrate Dababase & Collect Static files

python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic

9 Reload and everything should be working all right now