pythondjangodockerwagtail

Reverse for 'wagtailadmin_explore' with arguments '('',)' not found


I have been trying to rebuild my Wagtail website as a docker container to be run on Fargate. For reasons, I started from scratch and rebuilt all my dependencies. After pip installing everything, the site works fine locally using venv.

However, I get strange errors when launching the in the Docker container. Can anyone help me identify a fix for this?

I get the following error at /admin:

Error during template rendering
In template /usr/local/lib/python3.8/site-packages/wagtail/admin/templates/wagtailadmin/home/site_summary_pages.html, error at line 4

Reverse for 'wagtailadmin_explore' with arguments '('',)' not found. 1 pattern(s) tried: ['admin/pages/(?P<parent_page_id>[0-9]+)/$']
1   {% load i18n wagtailadmin_tags %}
2   
3   <li class="icon icon-doc-empty-inverse">
4       <a href="{% url 'wagtailadmin_explore' root_page.pk %}">
5           {% blocktrans count counter=total_pages with total_pages|intcomma as total %}
6               <span>{{ total }}</span> Page <span class="visuallyhidden">created in {{ site_name }}</span>
7           {% plural %}
8               <span>{{ total }}</span> Pages <span class="visuallyhidden">created in {{ site_name }}</span>
9           {% endblocktrans %}
10      </a>
11  </li>
12  

My base image for Docker is Python 3.8 Alpine. I've diffed a pip freeze for both local and Docker and the versions of the packages are the same. All migrations run on both docker and local.

After discussion in the comments below, it seems that the issue might be with my Docker files. I am deploying a Python 3.8 container for Wagtail and a postgres container for the database. When I drop then recreate the DB on the postgres container the error goes away. So something is going wrong with the migrations when the site is deployed, but I don't know what it could be...

My docker file is below:

# pull official base image
FROM python:3.8.0-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev \
    musl-dev bash libffi-dev zlib zlib-dev make jpeg jpeg-dev

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh

# copy project
COPY . /usr/src/app/

# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

and my entrypoint file:

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi

python manage.py flush --no-input
python manage.py migrate
python manage.py collectstatic --no-input --clear
python manage.py runserver 0.0.0.0:8000

exec "$@"

and my docker compose file:

version: '3.7'

services: 
  web:
    build: ./
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./:/usr/src/app/
    ports: 
      - 8000:8000
    env_file: 
      - ./.env.dev

  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=blog_project

volumes:
  postgres_data:

Solution

  • This is probably because you wiped your database (or some part of it) after migrating. The wagtailcore_page table is probably empty when it shouldn't be.

    With wagtail, when you run python manage.py migrate, the migrations will not only create the needed tables, they'll also fill it with an initial root page from where you build your site structure. When you remove this page, you'll get the error you specified.

    {% url 'wagtailadmin_explore' root_page.pk %} is the culprit here. Wagtail tries to revers the wagtailadmin_explore url with root_page.pk, but in your case root_page is None.

    Simply running python manage.py migrate again will not work, because Wagtail will assume its migrations have already run, and it will not create a root page again. To fix this, either remove your database entirely and run python manage.py migrate, or passing a correct database into your Docker container.

    It's best to use the wagtail interface to remove, restructure or change pages. I've had quite some issues in the past where I broke my Wagtail installation.

    EDIT:

    I think you should delete python manage.py flush --no-input in entrypoint.sh. This file is executed every time you run your container, which means your database is flushed everytime you call docker run or docker-compose up.

    flush will clear all your data, but keep the actual tables. If you run migrate, then flush and then migrate again, it will find no migrations to apply (Because they have already been applied), so the migrations creating your root page will not be run either.

    I'm not exactly sure why you would want to flush your database everytime anyway, especially not in a Docker container. If you want to start with a fresh database, it's best to run this as a one-off command, and it's best to just drop all tables through psql.