This example shows how to use Django 5 on PythonAnywhere
Last updated: 27-10-2025
https://persteen.pythonanywhere.com/
- Download Python from the official website Python
- Make sure that you have installed Python by the command in Powershell: "python --version"
- Download the Python extension for Visual Studio Code which automatically include the Pylance extionsion
- Download / fork this Django Starter Website from my GitHub
- Create the virtual environment ".venv" for the Django Web App by Powershell or by VS Code
- Virtual Environment by VS Code: "View - Command Palette - Python Create Environment"
- 
python -m venv .venv 
- 
cd .venv 
- 
Scripts/activate 
- 
Copy requirements.txt to .venv 
- 
(.venv) pip install -r requirements.txt 
- 
(.venv) pip freeze > requirements.txt 
- 
(.venv) cd ../ 
- 
(.venv) python manage.py runserver 
- 
mkvirtualenv --python=python3.12 myvirtualenv 
- 
Copy requirements.txt to .myvirtualenv 
- 
$ workon myvirtualenv 
- 
(.myvirtualenv) pip install whitenoise=6.11.0 
- 
(.myvirtualenv) pip install PyMySQL=1.1.2 
- 
(.myvirtualenv) pip install python-dotenv=0.20.0 
- 
(.myvirtualenv) pip install Django=5.1.11 
- 
(.myvirtualenv) cd ../ 
When starting the Django Website from the Virtual Enviroment (.venv) you will notice that Django 5.1.11 will start. Otherwise you can use the Global Django if you have one installed by running:
- python manage.py runserver
Our Django application, example is configured as an installed application in mysite/settings.py:
# mysite/settings.py
INSTALLED_APPS = [
    # ...
    'example',
]We allow all subdomains in ALLOWED_HOSTS, in addition to 127.0.0.1:
# mysite/settings.py
ALLOWED_HOSTS = ['*']The wsgi module must use a public variable named app to expose the WSGI application:
# mysite/wsgi.py
app = get_wsgi_application()The corresponding WSGI_APPLICATION setting is configured to use the app variable from the mysite.wsgi module:
# mysite/settings.py
WSGI_APPLICATION = 'mysite.wsgi.app'This Django example uses the Web Server Gateway Interface (WSGI) with Django to enable handling requests on PythonAnywhere
There are severals views in example/views.py which load HTML Django Templates templates:
The views are exposed a URL through example/urls.py:
# example/urls.py
from django.urls import path
from example.views import index
from example.views import about
from example.views import me
urlpatterns = [
    path('', index),
    path('about', about),
    path('me', me),
]Finally, it's made accessible to the Django server inside mysite/urls.py:
# mysite/urls.py
from django.urls import path, include
urlpatterns = [
    ...
    path('', include('example.urls')),
]To use templates create the dir 'templates' at root level and put the HTML files there
There is only one Django App in the Project and the dir 'templates' can be at root level
Tell Django where to look for Templates by example/settings.py:
Find the section TEMPLATES = [] and add the dir of the Templates
'DIRS': [BASE_DIR / 'templates']
python manage.py runserverYour Django application is now available at http://127.0.0.1:8000/.
The Admin Backend is using a MySQL Database at PythonAnywhere for Production, but is able to use a SQLite for Production as well
To connect to the MySQL use "mysqlclient" installed at PythonAnywhere or install the Python package "pymysql" and the packages from the requirements.txt if you are using a virtual invironment at PythonAnywhere
pip install -r requirements.txtIf using "pymysql" take a look at the files needed for connecting to MySQL: mysite/mysql_setup.py and
mysite/__init__.py
Create a Super User for the Admin Backend in the MySQL or SQLite
python manage.py createsuperuserMake the Migration to the MySQL or SQLite
python manage.py makemigrations
python manage.py migrateYou will need to do the Migration at first and when / if you will add, update or delete models.py which this Django Web App does not use
For using a SQLite developing / locally make the config in the setting file mysite/settings.py
Find the section DATABASES = {} and add support for SQLite and comment out the MySQL
There is only one Django App in the Project and the dir 'static' and 'assets' can be created at root level
Make sure that the Python package 'whitenoise' is installed from the requirements.txt if you are using a virtual invironment at PythonAnywhere
Note: Make sure you have the line 'whitenoise.middleware.WhiteNoiseMiddleware' in the
MIDDLEWARE = [] at the mysite/settings.py along with the other packages
Finally, take a look at mysite/settings.py:
Find where Django now looks for the static files
STATIC_URL = 'static/'
Where you put your static files in the dir 'static'
STATIC_ROOT = BASE_DIR/'static'
The files in the dir 'asset' will be copied to the dir 'static' after running
python manage.py collectstaticNote: The above command may not be needed as this Starter dont have a Django Admin backend
Type the URL in your Browser after deployment to Vercel
https:// your project at pythonanywhere/static/pso-django.jpg
or the URL when running locally
http://127.0.0.1:8000/static/pso-django.jpg
If everything is fine my photo will be displayed
The CSS files can be tested the same way like the .jpg above
Now you can use the images and CSS in your Templates
python manage.py runserverThe Django application is now available at http://127.0.0.1:8000
Make sure that your static files are ready by running
python manage.py collectstaticNote: The above command may not be needed as this Starter dont have a Django Admin backend
Make sure to set Debug = False in the file mysite/settings.py
Copy your local files to PythonAnywhere
Add the simple Model "Post" to be administrated by the Admin Backend and displayed by the Frontend.
- 
Create a file example/models.pywith your new Model Post
- 
Make a regitration of your Model in example/admin.py
- 
Create the View for handling the Posts example/views.py
- 
Add the view / template blog in example/urls.py
- 
Create a Template for display the Posts templates/blog.html
- 
Create a folder with the path: example/migrationsand run the command:
python manage.py makemigrations exampleNote: It is important to add the name of the app in the command example !!!
This command will create a file for the migration of the Model to a Table in the SQLite or MySQL DB
- Now run the command:
python manage.py migrateThis will create the Table Post in the DB and you are now ready for administrate the Posts by the Django Admin Backend
Happy use of Django :-)