|
| 1 | +# Wagtail Installation and Configuration |
| 2 | + |
| 3 | +In this section, we will cover how to install and configure Wagtail in an existing project. The experience will |
| 4 | +slightly be different from just launching a new Wagtail project as we already have a working project that we need to |
| 5 | +improve by adding the powerful and wonderful capabilities of Wagtail. |
| 6 | + |
| 7 | +## Wagtail Installation |
| 8 | +To install Wagtail, type the following command in the command prompt: |
| 9 | + |
| 10 | +``` |
| 11 | +(myvenv) ~/djangogirls$ pip install wagtail>=5.2,<5.3 |
| 12 | +``` |
| 13 | + |
| 14 | +Now that we have installed Wagtail, we need to add it to our `requirements.txt` so that we can keep track of our |
| 15 | +requirements and we can easily install all our project dependencies when we deploy the changes to our website. To |
| 16 | +update the `requirements.txt`, type the following command in the command prompt: |
| 17 | + |
| 18 | +``` |
| 19 | +(myvenv) ~/djangogirls$ pip freeze > requirements.txt |
| 20 | +``` |
| 21 | + |
| 22 | +## Wagtail Settings |
| 23 | +Now, we should update the Wagtail configuration in `mysite/settings.py`. First, add Wagtail and its associated apps to `INSTALLED_APPS`, like this: |
| 24 | + |
| 25 | +```python |
| 26 | +INSTALLED_APPS = [ |
| 27 | + "django.contrib.admin", |
| 28 | + "django.contrib.auth", |
| 29 | + "django.contrib.contenttypes", |
| 30 | + "django.contrib.sessions", |
| 31 | + "django.contrib.messages", |
| 32 | + "django.contrib.staticfiles", |
| 33 | + "wagtail.contrib.forms", |
| 34 | + "wagtail.contrib.redirects", |
| 35 | + "wagtail.embeds", |
| 36 | + "wagtail.sites", |
| 37 | + "wagtail.users", |
| 38 | + "wagtail.snippets", |
| 39 | + "wagtail.documents", |
| 40 | + "wagtail.images", |
| 41 | + "wagtail.search", |
| 42 | + "wagtail.admin", |
| 43 | + "wagtail", |
| 44 | + "modelcluster", |
| 45 | + "taggit", |
| 46 | + "blog", |
| 47 | +] |
| 48 | +``` |
| 49 | + |
| 50 | +So far we have only been using Django's inbuilt middleware. However to use Wagtail, we need to add its custom middleware. |
| 51 | +We need to add the following line to the `MIDDLEWARE` setting: |
| 52 | + |
| 53 | +```python |
| 54 | + "wagtail.contrib.redirects.middleware.RedirectMiddleware", |
| 55 | +``` |
| 56 | + |
| 57 | +The `MIDDLEWARE` setting should now look like this: |
| 58 | + |
| 59 | +```python |
| 60 | +MIDDLEWARE = [ |
| 61 | + "django.middleware.security.SecurityMiddleware", |
| 62 | + "django.contrib.sessions.middleware.SessionMiddleware", |
| 63 | + "django.middleware.common.CommonMiddleware", |
| 64 | + "django.middleware.csrf.CsrfViewMiddleware", |
| 65 | + "django.contrib.auth.middleware.AuthenticationMiddleware", |
| 66 | + "django.contrib.messages.middleware.MessageMiddleware", |
| 67 | + "django.middleware.clickjacking.XFrameOptionsMiddleware", |
| 68 | + "wagtail.contrib.redirects.middleware.RedirectMiddleware", |
| 69 | +] |
| 70 | +``` |
| 71 | + |
| 72 | +We also need the `MEDIA_ROOT` and `MEDIA_URL` settings as shown below: |
| 73 | + |
| 74 | +```python |
| 75 | +MEDIA_ROOT = BASE_DIR / 'media' |
| 76 | +MEDIA_URL = '/media/' |
| 77 | +``` |
| 78 | + |
| 79 | +The last setting to be added is the `WAGTAIL_SITE_NAME`. This will be displayed on the main dashboard of the |
| 80 | +Wagtail admin backend. This should look like below: |
| 81 | + |
| 82 | +```python |
| 83 | +WAGTAIL_SITE_NAME = 'Django Girls Blog' |
| 84 | +``` |
| 85 | + |
| 86 | +## URL Configuration |
| 87 | +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 |
| 88 | +views. We will add the following lines to `mysite/urls.py` right below the imports we have. The Wagtail imports should |
| 89 | +be below the Django imports so that we follow best practices for handling imports. This is because Wagtail imports are |
| 90 | +**third party** imports so should be below the main Django imports. |
| 91 | + |
| 92 | +```python |
| 93 | +from wagtail.admin import urls as wagtailadmin_urls |
| 94 | +from wagtail import urls as wagtail_urls |
| 95 | +``` |
| 96 | + |
| 97 | +Then we need to add the following lines to `urlspatterns` list to include Wagtail URLs. |
| 98 | +```python |
| 99 | + path('cms/', include(wagtailadmin_urls)), |
| 100 | + path('pages/', include(wagtail_urls)), |
| 101 | +``` |
| 102 | + |
| 103 | +Wagtail comes with its own custom admin interface provided by `wagtailadmin_urls` which we will be able to access by |
| 104 | +visiting the URL`/cms/`. This is different from the Django admin interface provided by `django.contrib.admin`we have |
| 105 | +been accessing by the `/admin/` URL. In a typical Wagtail only project, the admin site would be at `/admin/` but |
| 106 | +because we are adding Wagtail to an already existing Django project, this would clash with our , admin URL so we are |
| 107 | +using `/cms/` to access the Wagtail admin interface and keep using `/admin/` to access the Django admin interface. |
| 108 | + |
| 109 | +Wagtail also comes with a user interface for serving Wagtail pages which is provided for by `wagtail_urls` and can be |
| 110 | +accessed by visiting the `/pages/` URL. In our current setup, Wagtail will handle Wagtail URLs under `/pages/` and leave |
| 111 | +the root and admin URLs to be handled by our normal Django project. Alternatively, we can have Wagtail serve all |
| 112 | +URLs, which we will work towards in this project, but for now, we will leave things as they are. |
| 113 | + |
| 114 | +Wagtail also has `wagtail.documents` to be used for documents management which we could also import as |
| 115 | +`wagtaildocs_urls`. However, since we will not be managing any documents for now, we can skip that and just add the two |
| 116 | +Wagtail URLs. |
| 117 | + |
| 118 | +We also need to add the media URLs to our `mysite/urls.py` to enable us to serve user uploaded files by adding the |
| 119 | +following lines to the top of the file: |
| 120 | + |
| 121 | +```python |
| 122 | +from django.conf import settings |
| 123 | +from django.conf.urls.static import static |
| 124 | +``` |
| 125 | +and then add the line below to the end of the file, right after the closing brace for `urlpatterns` to add it to the |
| 126 | +list of URLs: |
| 127 | + |
| 128 | +```python |
| 129 | ++ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
| 130 | +``` |
| 131 | + |
| 132 | +Our `mysite/urls.py` should now look like below: |
| 133 | + |
| 134 | +{% filename %}mysite/urls.py{% endfilename %} |
| 135 | +```python |
| 136 | +from django.conf import settings |
| 137 | +from django.conf.urls.static import static |
| 138 | +from django.contrib import admin |
| 139 | +from django.urls import include, path |
| 140 | + |
| 141 | +from wagtail.admin import urls as wagtailadmin_urls |
| 142 | +from wagtail import urls as wagtail_urls |
| 143 | + |
| 144 | +urlpatterns = [ |
| 145 | + path("admin/", admin.site.urls), |
| 146 | + path('', include('blog.urls')), |
| 147 | + path('cms/', include(wagtailadmin_urls)), |
| 148 | + path('pages/', include(wagtail_urls)), |
| 149 | +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
| 150 | +``` |
| 151 | + |
| 152 | +After this, we need to run migrations to create models for Wagtail as shown below: |
| 153 | + |
| 154 | +``` |
| 155 | +(myvenv) ~/djangogirls$ python manage.py runserver |
| 156 | +``` |
| 157 | + |
| 158 | +We should now be able to get the following when we visit `http://127.0.0.1:8000/cms/`: |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +We should also get the following when we visit `http://127.0.0.1:8000/pages/`: |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +Our existing root URL for the blog at `http://127.0.0.1:8000` should also still be working |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +as well as our Django admin interface at `http://127.0.0.1:8000/admin/`. |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +Looks like we are all set to use Wagtail in our project. We are ready to move to the next stage where will integrate our |
| 179 | +blog to use Wagtail. |
| 180 | + |
| 181 | +Let's keep going! |
0 commit comments