pythonanywhere Hosting of Django Application on GitHub

Notes on the bare minimum to set up a Web application on pythonanywhere (free tier) that is source-controlled on Github.

Create Django application locally

  • Create a Django application using the default SQLite DB. Follow the Django tutorial such as https://docs.djangoproject.com/en/5.2/intro/tutorial01/ if necessary.
  • Make sure it runs (e.g. python manage.py runserver 0.0.0.0:8000 will start the app accessible from http://localhost:8000/ ).
  • Modify the ALLOWED_HOSTS in settings.py to include <your user name>.pythonanywhere.com since that’s the URL that pythonanywhere will assign to a free account:
    • ALLOWED_HOSTS = [

      "van.pythonanywhere.com", # For example
      ]

Set up to GitHub repository

Create a Web application on pythonanywhere

  • Select Python and Django versions to match the project.
  • For the project name, matching that with the GitHub repository name is recommended (though not necessary). For this example, I’ll use djtest.
  • Test the application (e.g. https://van.pythonanywhere.com) and verify the rocket page is up.

Modify Web application to the GitHub project

  • Open a Console in pythonanywhere. Ensure that the subdirectory for the Web application (~/djtest in this example) is there.
  • Verify that git is available (e.g. git --version)
  • REMOVE the application directory and replace it with the project from GitHub:
    • rm -rf djtest
      git clone https://github.com/.../djtest.git
      • Use your GitHub user name and Personal Access Token as the password.
      • NOTE that I named the GitHub project djtest. This matches the Web application name in pythonanywhere.

Modify Web application to point to the GitHub project

  • Bring up the Web application page in pythonanywhere.
  • Update the application configuration to point to the GitHub project.
    • Under “Code:,” modify the Source code directory to point to the subdirectory created by the git clone.
    • Modify the xxxx_pythonanywhere_com_wsgi.py file (just click the link to edit the file) so that:
      • project_home points to the Web application subdirectory (e.g. /home/.../djtest).
      • os.environ['DJANGO_SETTINGS_MODULE'] points correctly to the settings.py file in the project (e.g. 'mysite.settings').
    • Modify Static files to point to the correct subdirectories.
      • For my example, they are:
        • /static/ -> /home/.../djtest/static
        • /media/ -> /home/.../djtest/media
  • Reload the Web application.
  • NOTE: this is why matching the Web application name in pythonanywhere to the Github repository name helps. When they match, fewer of these need to be modified.

Updates and Deployment to the Web application

Changes to the GitHub repository are not pushed into pythonanywhere automatically. Therefore, the deployment is manual:

  • Make changes to the GitHub repository via pull requests, etc. and merge into default branch (e.g. master).
  • Log into pythonanywhere and open up a console. Update the code to latest from GitHub:
    • cd djtest
      git pull
    • Use the GitHub user name and personal access token as before to access GitHub.
      • The usual git config
  • Reload the Web application.