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.
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"
Then create an initial requirements file with:
pipenv requirements > requirements.txt
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 https://github.com/github/gitignore/blob/main/Python.gitignore
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
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:
rm webapp/.dockerignore webapp/Dockerfile webapp/requirements.txt
Move the manage.py file to the root of the project:
mv webapp/manage.py .
Move the all the files and folders from the webapp/webapp directory up one level to the webapp directory:
mv webapp/webapp/* webapp
Delete the empty webapp/webapp directory:
rm -rf webapp/webapp
Alter import paths and settings
Open webapp/urls.py and change the import path for:
from search import views as search_views # to from webapp.search import views as search_views
Open webapp/settings/base.py and change the app names for home and search in INSTALLED_APPS to:
INSTALLED_APPS = [ "webapp.home", "webapp.search", ... ]
The project file structure should look like this:
│ ├── home
│ ├── search
│ ├── settings
│ ├── static
│ ├── templates
│ ├── __init__.py
│ ├── urls.py
│ └── wsgi.py
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 manage.py migrate python manage.py createsuperuser
Then run the local development server:
python manage.py runserver
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.