Skip to content

Commit a2bff42

Browse files
authored
Merge pull request #163 from amakarudze/add-wagtail
Add Wagtail installation instructions
2 parents 48e19f8 + bf9466c commit a2bff42

29 files changed

+2992
-1048
lines changed

en/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Current tutorials are:
1212
* [Homework: add more to your website!](homework/README.md)
1313
* [Homework: secure your website](authentication_authorization/README.md)
1414
* [Homework: create comment model](homework_create_more_models/README.md)
15+
* [Homework: Add Wagtail to your website](add_wagtail_to_your_website/README.md)
1516
* [Optional: PostgreSQL installation](optional_postgresql_installation/README.md)
1617

1718
## Contributing

en/SUMMARY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* [Homework: add more to your website!](homework/README.md)
55
* [Homework: secure your website](authentication_authorization/README.md)
66
* [Homework: create comment model](homework_create_more_models/README.md)
7+
* [Homework: Add Wagtail to your website](add_wagtail_to_your_website/README.md)
8+
* [Wagtail Installation](add_wagtail_to_your_website/install_wagtail/README.md)
9+
* [Wagtail - Adding the Homepage](add_wagtail_to_your_website/wagtail_integration_adding_homepage/README.md)
10+
* [Wagtail - Adding Posts](add_wagtail_to_your_website/wagtail_integration_adding_posts/README.md)
11+
* [Wagtail - Change Website Homepage](add_wagtail_to_your_website/change_website_homepage/README.md)
712
* [Optional: PostgreSQL installation](optional_postgresql_installation/README.md)
813
* [Optional: Domain](domain/README.md)
914

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Homework: Add Wagtail to your website
2+
3+
> Part of this chapter is based on Wagtail’s official [Getting Started tutorial](https://docs.wagtail.org/en/stable/getting_started/tutorial.html).
4+
5+
We can make our blog even more professional by adding Wagtail to our website while at the same time we are
6+
developing our coding skills in Python and Django. Wagtail is an open source content management system built on
7+
Django, with a strong community and commercial support. It's focused on user experience,
8+
and offers precise control for designers and developers.
9+
10+
The website we built through following the tutorial is a simplified content management system because it allows us
11+
to add and manage content as well as serve it to the visitors of our blog. Wagtail on the other hand is a more complex
12+
content management system with more advanced features. Adding Wagtail to our website will enable us to learn more about
13+
Django and see what cool applications we can build with it.
14+
15+
In this part of the tutorial, we will cover the following aspects:
16+
- [Installing Wagtail and Configuring Wagtail](install_wagtail/README.md)
17+
- [Adding the Homepage](wagtail_integration_adding_homepage/README.md)
18+
- [Adding Posts](wagtail_integration_adding_posts/README.md)
19+
- [Change Website Homepage](change_website_homepage/README.md)
20+
21+
So let's get started!
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Change Website Homepage to Wagtail Blog Homepage
2+
3+
The Wagtail admin has a nicer and a more user-friendly interface compared to the default Django admin user interface.
4+
For this reason, we would rather use our Wagtail blog as our main blog and ignore the blog we created in the main
5+
tutorial.
6+
7+
We will not remove our initial blog, we will leave it as it is as we may need to use it for more learning. We will
8+
however change the root of our project to point to our Wagtail blog. Make the following changes to your
9+
`mysite/urls.py` as shown below:
10+
11+
First change the `URL` for your first blog to the following:
12+
13+
```python
14+
path('blog/', include('blog.urls')),
15+
```
16+
17+
Next change the `URL` for the Wagtail blog to the following:
18+
19+
```python
20+
path('', include(wagtail_urls)),
21+
```
22+
23+
Your `mysite/urls.py` should now look like below:
24+
25+
{% filename %} mysite/urls.py {% endfilename %}
26+
```python
27+
from django.conf import settings
28+
from django.conf.urls.static import static
29+
from django.contrib import admin
30+
from django.urls import include, path
31+
32+
from wagtail.admin import urls as wagtailadmin_urls
33+
from wagtail import urls as wagtail_urls
34+
35+
urlpatterns = [
36+
path('admin/', admin.site.urls),
37+
path('blog/', include('blog.urls')),
38+
path('cms/', include(wagtailadmin_urls)),
39+
path('', include(wagtail_urls)),
40+
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
41+
42+
```
43+
44+
This will make your Wagtail accessible by visiting `http://127.0.0.1:8000` as shown below
45+
46+
![New Website Homepage](images/wagtail_as_root.png)
47+
48+
and your first blog will now be at `http://127.0.0.1:8000/blog/` as shown below.
49+
50+
![Old Blog's New URL](images/old_blog_url.png)
51+
52+
# Committing our changes to GitHub
53+
54+
Now that we have managed to get Wagtail working well with out blog, we can deploy our changes to our live website
55+
which we deployed to PythonAnywhere earlier on during the main tutorial. To do this, we need to first push our code to
56+
GitHub as we have done before by typing the following in our command line:
57+
58+
```
59+
(myvenv) ~/djangogirls$ git status
60+
```
61+
62+
This will list all the files we have changed. Once we have confirmed those are the changes we have made, we can go
63+
ahead and add the files to be committed by typing the following command:
64+
65+
```
66+
(myvenv) ~/djangogirls$ git add .
67+
```
68+
69+
Next we need to add a commit message for our changes to be saved by typing the following command:
70+
71+
```
72+
(myvenv) ~/djangogirls$ git commit -m "Add Wagtail to our blog"
73+
```
74+
75+
Lastly, we need to push the commit to GitHub by typing the following command:
76+
77+
```
78+
(myvenv) ~/djangogirls$ git push origin HEAD
79+
```
80+
81+
This will push our new changes to GitHub and we are now ready to deploy these changes to PythonAnywhere!
82+
83+
# Deploying the changes to PythonAnywhere
84+
To deploy our changes to [PythonAnywhere](https://pythonanywhere.com) and click `Consoles` and click on
85+
`Bash Console in virtualenv` to open a new Bash console with an activated virtual environment and then type in the
86+
following command:
87+
88+
```
89+
$ git pull origin HEAD
90+
```
91+
92+
This command will pull your changes from GitHub to your PythonAnywhere project.
93+
94+
Next, you need to update the requirements for your project by installing Wagtail and its dependencies. Since we
95+
updated our requirements file, you only need to type in the following command to install the required packages:
96+
97+
```
98+
$ pip install -r requirements.txt
99+
```
100+
101+
Next step is to run migrations for the new models and create the necessary tables in the database. To do this, type the
102+
following command:
103+
104+
```
105+
$ python manage.py migrate
106+
```
107+
108+
Next, you need to run the `collectstatic` command to copy the static files required by Wagtail to the `static` root
109+
folder specified in our `settings.py` file.
110+
111+
```
112+
$ python manage.py collectstatic
113+
```
114+
115+
Lastly, go to `Web` tab and reload your webapp by clicking the `Reload <your-username>.pythonanywhere.com` button and
116+
open `https://<your-username.pythonanywhere.com` in another tab to see your blog now using Wagtail CMS.
117+
118+
You should get the `Welcome to your new Wagtail site!` on your homepage to show that Wagtail is now working as your
119+
website root.
120+
121+
To change the root page of your Wagtail blog visit `https://<your-username.pythonanywhere.com/cms/` and login.
122+
123+
Then follow the steps in [Changing the Wagtail Homepage](../wagtail_integration_adding_homepage/README.md#changing-homepage) section of this tutorial to create a new homepage and change it to be the root of the blog.
124+
125+
To create new posts, follow instructions in the [Adding posts](../wagtail_integration_adding_posts/README.md#adding-blog-posts) section of this tutorial.
126+
127+
After adding your posts, you should see that only posts/pages that have been published are the ones displayed on your blog.
128+
In other words, Wagtail only shows posts which are `live` and a post is only `live` after you click the `Publish` button.
129+
The `Save draft` button merely saves the post without publishing it or making it live.
130+
131+
Also, take note that `live` posts are displayed in the order in which they are published, that is, they are ordered by date.
132+
In Django, the statement to get blog posts would look like `BlogPage.objects.filter(published=True).order_by(published_date)`.
133+
134+
That's all for this tutorial. If you want to learn more about Wagtail, you can read the
135+
[Wagtail documentation](https://guide.wagtail.org/en-latest/) or search for more tutorials on Wagtail online.
136+
137+
Happy coding!
Loading
Loading
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
![Wagtail CMS homepage](images/wagtail_cms.png)
161+
162+
163+
We should also get the following when we visit `http://127.0.0.1:8000/pages/`:
164+
165+
![Wagtail homepage](images/wagtail_pages.png)
166+
167+
168+
Our existing root URL for the blog at `http://127.0.0.1:8000` should also still be working
169+
170+
![Blog homepage](images/blog_home.png)
171+
172+
173+
as well as our Django admin interface at `http://127.0.0.1:8000/admin/`.
174+
175+
![Django admin interface](images/django_admin.png)
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!
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)