Skip to content

Python and Django Website hosted at PythonAnywhere using MySQL and SQLite. Ready for serving static files and using Django models

Notifications You must be signed in to change notification settings

persteenolsen/django-pythonanywhere

Repository files navigation

Python + Django + MySQL + Models + PythonAnywhere

This example shows how to use Django 5 on PythonAnywhere

Last updated: 27-10-2025

Demo at PythonAnywhere

https://persteen.pythonanywhere.com/

Installing

  • 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"

Install with Python commands by Powershell locally at Windows 10

  • 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

Install with Python commands by the Bash Console at PythonAnywhere

  • 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

How it Works

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

Routing

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')),
]

Templates

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']

Running Locally

python manage.py runserver

Your Django application is now available at http://127.0.0.1:8000/.

The Admin Backend and Databases

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.txt

If 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 createsuperuser

Make the Migration to the MySQL or SQLite

python manage.py makemigrations
python manage.py migrate

You 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

Static files for the Frontend

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'

Additional directory from which to load static files if wanted

The files in the dir 'asset' will be copied to the dir 'static' after running

python manage.py collectstatic

Note: The above command may not be needed as this Starter dont have a Django Admin backend

Check that Django is serving static files by URL

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

Running Locally and take a look at the Website

python manage.py runserver

The Django application is now available at http://127.0.0.1:8000

Deployment to PythonAnywhere

Make sure that your static files are ready by running

python manage.py collectstatic

Note: 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

Models

Add the simple Model "Post" to be administrated by the Admin Backend and displayed by the Frontend.

  • Create a file example/models.py with 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/migrations and run the command:

python manage.py makemigrations example

Note: 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 migrate

This 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 :-)

About

Python and Django Website hosted at PythonAnywhere using MySQL and SQLite. Ready for serving static files and using Django models

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages