Wagtail Installation and Configuration
In this section, we will cover how to install and configure Wagtail in an existing project. The experience will slightly be different from just launching a new Wagtail project as we already have a working project that we need to improve by adding the powerful and wonderful capabilities of Wagtail.
Wagtail Installation
To install Wagtail, type the following command in the command prompt:
Now that we have installed Wagtail, we need to add it to our requirements.txt
so that we can keep track of our requirements and we can easily install all our project dependencies when we deploy the changes to our website. To update the requirements.txt
, type the following command in the command prompt:
Wagtail Settings
Now, we should update the Wagtail configuration in mysite/settings.py
. First, add Wagtail and its associated apps to INSTALLED_APPS
, like this:
So far we have only been using Django's inbuilt middleware. However to use Wagtail, we need to add its custom middleware. We need to add the following line to the MIDDLEWARE
setting:
The MIDDLEWARE
setting should now look like this:
We also need the MEDIA_ROOT
and MEDIA_URL
settings as shown below:
The last setting to be added is the WAGTAIL_SITE_NAME
. This will be displayed on the main dashboard of the Wagtail admin backend. This should look like below:
URL Configuration
We also need to add Wagtail URLs to our root URLs file, that is mysite/urls.py
so that we are able to access Wagtail views. We will add the following lines to mysite/urls.py
right below the imports we have. The Wagtail imports should be below the Django imports so that we follow best practices for handling imports. This is because Wagtail imports are third party imports so should be below the main Django imports.
Then we need to add the following lines to urlspatterns
list to include Wagtail URLs.
Wagtail comes with its own custom admin interface provided by wagtailadmin_urls
which we will be able to access by visiting the URL/cms/
. This is different from the Django admin interface provided by django.contrib.admin
we have been accessing by the /admin/
URL. In a typical Wagtail only project, the admin site would be at /admin/
but because we are adding Wagtail to an already existing Django project, this would clash with our , admin URL so we are using /cms/
to access the Wagtail admin interface and keep using /admin/
to access the Django admin interface.
Wagtail also comes with a user interface for serving Wagtail pages which is provided for by wagtail_urls
and can be accessed by visiting the /pages/
URL. In our current setup, Wagtail will handle Wagtail URLs under /pages/
and leave the root and admin URLs to be handled by our normal Django project. Alternatively, we can have Wagtail serve all URLs, which we will work towards in this project, but for now, we will leave things as they are.
Wagtail also has wagtail.documents
to be used for documents management which we could also import as wagtaildocs_urls
. However, since we will not be managing any documents for now, we can skip that and just add the two Wagtail URLs.
We also need to add the media URLs to our mysite/urls.py
to enable us to serve user uploaded files by adding the following lines to the top of the file:
and then add the line below to the end of the file, right after the closing brace for urlpatterns
to add it to the list of URLs:
Our mysite/urls.py
should now look like below:
```python from django.conf import settings from django.conf.urls.static import static from django.contrib import admin from django.urls import include, path
from wagtail.admin import urls as wagtailadmin_urls from wagtail import urls as wagtail_urls
urlpatterns = [ path("admin/", admin.site.urls), path('', include('blog.urls')), path('cms/', include(wagtailadmin_urls)), path('pages/', include(wagtail_urls)), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
(myvenv) ~/djangogirls$ python manage.py runserver
Last updated
Was this helpful?