Deploy Wagtail CMS to PythonAnywhere

Initial repo and starting a new Wagtail project

Create a new repo on your preferred provider. Github can add some files for you when starting a new project, I decided to add a README file and a gitignore file for Python.

Clone the newly created repo to your local development machine. It will have a remote called origin.

Later on I'll create a remote called production which I'll use to deploy the site to PythonAnywhere.

Python dependencies

There are a few ways to manage python dependencies, I'm using Pipenv but you can use your preferred method. The only caveat is that the production dependencies ideally need to be in a requirements.txt file when deploying to PythonAnywhere.

With Pipenv installed run:

pipenv install "wagtail>=5.1,<5.2" "mysqlclient>=2.2,<2.3" "python-dotenv>=1.0,<1.1" "django>=4.2,<5.0"
note code Copy code

Then create an initial requirements file with:

pipenv requirements > requirements.txt
note code Copy code

Start a Wagtail site

If you don't have one already, add a .gitignore file so we don't commit anything we shouldn't.

A good example of a python .gitignore file is

The .gitignore file should also contain the following at this stage.


Now create the initial Wagtail site called webapp or use your preferred name.

pipenv run wagtail start webapp
note code Copy code

Modify the file and folder layout a little

I like to alter the layout of the files and folders a little to suit my preferences. It's not necessary but I find it easier to work with. You can skip this step if you prefer but make sure you adjust any paths in the following steps.

Inside the webapp directory you can delete the following files as they are not used on PythonAnywhere:

  • .dockerignore
  • Dockefile
  • requirements.txt
rm webapp/.dockerignore webapp/Dockerfile webapp/requirements.txt
note code Copy code

Move the file to the root of the project:

mv webapp/ .
note code Copy code

Move the all the files and folders from the webapp/webapp directory up one level to the webapp directory:

mv webapp/webapp/* webapp
note code Copy code

Delete the empty webapp/webapp directory:

rm -rf webapp/webapp
note code Copy code

Alter import paths and settings

Open webapp/ and change the import path for:

from search import views as search_views
# to
from import views as search_views
note code Copy code

Open webapp/settings/ and change the app names for home and search in INSTALLED_APPS to:

note code Copy code

The project file structure should look like this:

├── venv
├── webapp
│ ├── home
│ ├── search
│ ├── settings
│ ├── static
│ ├── templates
│ ├──
│ ├──
│ └──
├── .gitignore
├── Pipfile
├── Pipfile.lock
├── requirements.txt

Run the Wagtail project locally

Run the Wagtail project locally to check that everything is working as expected, activate the virtual environment e.g. pipenv shell and run the following commands:

python migrate
python createsuperuser
note code Copy code

Then run the local development server:

python runserver
note code Copy code

Open a browser and go to http://localhost:8000 and you should see the default Wagtail home page.

Open a browser and go to http://localhost:8000/admin/ and login with the superuser credentials you just created.

The site is currently using sqlite3 database but later on it will be swapped out for a Mysql database.