2015-06-01 14:30:07 +00:00
Your first Wagtail site
=======================
2015-11-10 21:45:21 +00:00
.. note ::
This tutorial covers setting up a brand new Wagtail project. If you'd like to add Wagtail to an existing Django project instead, see :doc: `integrating_into_django` .
2015-06-17 15:19:07 +00:00
1. Install Wagtail and its dependencies::
2015-06-01 14:30:07 +00:00
2015-06-17 15:19:07 +00:00
pip install wagtail
2015-06-01 14:30:07 +00:00
2015-06-17 15:19:07 +00:00
2. Start your site::
2015-06-01 14:30:07 +00:00
2015-06-17 15:19:07 +00:00
wagtail start mysite
cd mysite
2015-06-01 14:30:07 +00:00
Wagtail provides a `` start `` command similar to
`` django-admin.py startproject `` . Running `` wagtail start mysite `` in
your project will generate a new `` mysite `` folder with a few
Wagtail-specific extras, including the required project settings, a
2015-06-01 15:23:13 +00:00
"home" app with a blank `` HomePage `` model and basic templates and a sample
"search" app.
2015-06-01 14:30:07 +00:00
2015-06-17 15:19:07 +00:00
3. Install project dependencies::
2015-06-01 14:30:07 +00:00
2015-06-17 15:19:07 +00:00
pip install -r requirements.txt
This ensures that you have the relevant version of Django for the project you've just created.
4. Create the database::
python manage.py migrate
2015-06-01 14:30:07 +00:00
If you haven't updated the project settings, this will be a SQLite
database file in the project directory.
2015-06-17 15:19:07 +00:00
5. Create an admin user::
2015-06-01 14:30:07 +00:00
2015-06-17 15:19:07 +00:00
python manage.py createsuperuser
2015-06-01 14:30:07 +00:00
2015-06-17 15:19:07 +00:00
6. `` python manage.py runserver `` If everything worked,
2015-06-01 14:30:07 +00:00
http://127.0.0.1:8000 will show you a welcome page
.. figure :: ../_static/images/tutorial/tutorial_1.png
:alt: Wagtail welcome message
2016-07-19 19:13:20 +00:00
You can now access the administrative area at http://127.0.0.1:8000/admin
2015-06-01 14:30:07 +00:00
.. figure :: ../_static/images/tutorial/tutorial_2.png
:alt: Administrative screen
Extend the HomePage model
-------------------------
2015-07-07 14:33:44 +00:00
Out of the box, the "home" app defines a blank `` HomePage `` model in `` models.py `` , along with a migration that creates a homepage and configures Wagtail to use it.
2015-06-01 14:30:07 +00:00
2015-07-07 14:33:44 +00:00
Edit `` home/models.py `` as follows, to add a `` body `` field to the model:
2015-06-01 14:30:07 +00:00
2015-10-05 09:01:03 +00:00
.. code-block :: python
2015-06-01 14:30:07 +00:00
2015-07-07 14:33:44 +00:00
from __future__ import unicode_literals
2015-06-01 14:30:07 +00:00
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
2015-07-07 14:33:44 +00:00
FieldPanel('body', classname="full")
2015-06-01 14:30:07 +00:00
]
`` body `` is defined as `` RichTextField `` , a special Wagtail field. You
2015-06-01 15:23:13 +00:00
can use any of the `Django core fields <https://docs.djangoproject.com/en/1.8/ref/models/fields/> `__ . `` content_panels `` define the
2015-10-14 16:28:49 +00:00
capabilities and the layout of the editing interface. :doc: `More on creating Page models. <../topics/pages>`
2015-06-01 14:30:07 +00:00
Run `` python manage.py makemigrations `` , then
`` python manage.py migrate `` to update the database with your model
changes. You must run the above commands each time you make changes to
the model definition.
2015-07-07 15:07:15 +00:00
You can now edit the homepage within the Wagtail admin area (go to Explorer, Homepage, then Edit) to see the new body field. Enter some text into the body field, and publish the page.
2015-07-07 14:33:44 +00:00
The page template now needs to be updated to reflect the changes made
to the model. Wagtail uses normal Django templates to render each page
type. It automatically generates a template filename from the model name
by separating capital letters with underscores (e.g. HomePage becomes
home\_page.html). Edit
`` home/templates/home/home_page.html `` to contain the following:
2015-06-01 14:30:07 +00:00
2015-10-05 09:01:03 +00:00
.. code-block :: html+django
2015-06-01 14:30:07 +00:00
{% extends "base.html" %}
2015-07-07 14:33:44 +00:00
{% load wagtailcore_tags %}
2015-06-01 14:30:07 +00:00
2015-06-01 15:23:13 +00:00
{% block body_class %}template-homepage{% endblock %}
2015-06-01 14:30:07 +00:00
{% block content %}
2015-10-13 08:52:16 +00:00
{{ page.body|richtext }}
2015-06-01 14:30:07 +00:00
{% endblock %}
.. figure :: ../_static/images/tutorial/tutorial_3.png
:alt: Updated homepage
2015-10-05 09:01:03 +00:00
Wagtail template tags
~~~~~~~~~~~~~~~~~~~~~
2016-03-06 04:14:51 +00:00
Wagtail provides a number of :ref: `template tags & filters <template-tags-and-filters>`
2015-10-05 09:01:03 +00:00
which can be loaded by including `` {% load wagtailcore_tags %} `` at the top of
your template file.
In this tutorial, we use the `richtext` filter to escape and print the contents
of a `` RichTextField `` :
.. code-block :: html+django
{% load wagtailcore_tags %}
2015-10-13 08:52:16 +00:00
{{ page.body|richtext }}
2015-10-05 09:01:03 +00:00
Produces:
.. code-block :: html
<div class="rich-text">
<p>
<b>Welcome</b> to our new site!
</p>
</div>
**Note:** You'll need to include `` {% load wagtailcore_tags %} `` in each
template that uses Wagtail's tags. Django will throw a `` TemplateSyntaxError ``
if the tags aren't loaded.
2015-06-01 14:30:07 +00:00
A basic blog
------------
2015-06-01 15:23:13 +00:00
We are now ready to create a blog. To do so, run
`` python manage.py startapp blog `` to create a new app in your Wagtail site.
Add the new `` blog `` app to `` INSTALLED_APPS `` in `` mysite/settings/base.py `` .
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
Blog Index and Posts
~~~~~~~~~~~~~~~~~~~~
Lets start with a simple index page for our blog. In `` blog/models.py `` :
.. code-block :: python
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
Run `` python manage.py makemigrations `` and `` python manage.py migrate `` .
Since the model is called `` BlogIndexPage `` , the default template name
(unless we override it) will be `` blog/templates/blog/blog_index_page.html: ``
.. code-block :: html+django
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-blogindexpage{% endblock %}
{% block content %}
<h1>{{ page.title }}</h1>
<div class="intro">{{ page.intro|richtext }}</div>
{% for post in page.get_children %}
<h2><a href="{% slugurl post.slug %}">{{ post.title }}</a></h2>
{{ post.specific.intro }}
{{ post.specific.body|richtext }}
{% endfor %}
{% endblock %}
Most of this should be familiar, but we'll explain `` get_children `` a bit later.
Note the `` slugurl `` tag, which is similar to Django's `` url `` tag but
intended to take a Wagtail slug as an argument.
In the Wagtail admin, create a `` BlogIndexPage `` under the Homepage,
make sure it has the slug "blog" on the Promote tab, and publish it.
You should now be able to access the url `` /blog `` on your site
(note how the slug from the Promote tab defines the page URL).
Now we need a model and template for our blog posts. In `` blog/models.py `` :
2015-06-01 14:30:07 +00:00
2015-10-05 09:01:03 +00:00
.. code-block :: python
2015-06-01 14:30:07 +00:00
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailsearch import index
class BlogPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
2016-03-22 06:41:19 +00:00
search_fields = Page.search_fields + [
2015-06-01 14:30:07 +00:00
index.SearchField('intro'),
index.SearchField('body'),
2016-03-22 06:41:19 +00:00
]
2015-06-01 14:30:07 +00:00
content_panels = Page.content_panels + [
FieldPanel('date'),
FieldPanel('intro'),
FieldPanel('body', classname="full")
]
2016-05-02 17:42:21 +00:00
.. note ::
On Wagtail versions before 1.5, `` search_fields `` needs to be defined as a tuple:
.. code-block :: python
search_fields = Page.search_fields + (
index.SearchField('intro'),
index.SearchField('body'),
)
2016-10-28 00:15:44 +00:00
Run `` python manage.py makemigrations `` and `` python manage.py migrate `` .
2015-06-21 22:39:47 +00:00
Create a template at `` blog/templates/blog/blog_page.html `` :
2015-10-05 09:01:03 +00:00
.. code-block :: html+django
2015-06-21 22:39:47 +00:00
{% extends "base.html" %}
2015-07-07 14:33:44 +00:00
{% load wagtailcore_tags %}
2015-06-21 22:39:47 +00:00
2015-08-03 13:35:32 +00:00
{% block body_class %}template-blogpage{% endblock %}
2015-06-21 22:39:47 +00:00
{% block content %}
2015-10-13 08:52:16 +00:00
<h1>{{ page.title }}</h1>
<p class="meta">{{ page.date }}</p>
2015-06-21 22:39:47 +00:00
2015-10-13 08:52:16 +00:00
<div class="intro">{{ page.intro }}</div>
2015-06-21 22:39:47 +00:00
2015-10-13 08:52:16 +00:00
{{ page.body|richtext }}
2016-10-28 00:15:44 +00:00
<p><a href="{{ page.get_parent.url }}">Return to blog</a></p>
2015-06-21 22:39:47 +00:00
{% endblock %}
2016-10-28 00:15:44 +00:00
Note the use of Wagtail's built-in `` get_parent() `` method to obtain the
URL of the blog this post is a part of.
Now create a few blog posts as children of `` BlogIndexPage. ``
Be sure to select type "BlogPage" when creating your posts.
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
.. figure :: ../_static/images/tutorial/tutorial_4a.png
:alt: Create blog post as child of BlogIndex
.. figure :: ../_static/images/tutorial/tutorial_4b.png
:alt: Choose type BlogPost
Wagtail gives you full control over what kinds of content can be created under
various parent content types, but we won't go into that here. By default,
any page type can be a child of any other page type.
2015-06-01 14:30:07 +00:00
.. figure :: ../_static/images/tutorial/tutorial_5.png
:alt: Page edit screen
2016-10-28 00:15:44 +00:00
You should now have the very beginnings of a working blog.
Access the `` /blog `` URL and you should see something like this:
.. figure :: ../_static/images/tutorial/tutorial_7.png
:alt: Blog basics
Titles should link to post pages, and a link back to the blog's
homepage should appear in the footer of each post page.
Parents and Children
~~~~~~~~~~~~~~~~~~~~
Much of the work you'll be doing in Wagtail revolves around the concept of hierarchical
"tree" structures consisting of nodes and leaves (see :doc: `../reference/pages/theory` ).
In this case, the `` BlogIndexPage `` is a "node" and individual `` BlogPage `` instances
are the "leaves".
Take another look at the guts of `` BlogIndexPage: ``
.. code-block :: html+django
{% for post in page.get_children %}
<h2>{{ post.title }}</h2>
{{ post.specific.intro }}
{{ post.specific.body|richtext }}
{% endfor %}
Every "page" in Wagtail can call out to its parent or children
from its own position in the hierarchy. But why do we have to
specify `` post.specific.title `` rather than `` post.title? ``
This has to do with the way we defined our model:
`` class BlogPage(Page): ``
The `` get_children() `` method got us a list of `` Page `` base classes. When we want to reference
properties of the instances that inherits from the base class, Wagtail provides the `` specific ``
method that retrieves the actual `` BlogPage `` record.
To tighten up template code like this, we could use Django's `` with `` tag:
.. code-block :: html+django
{% for post in page.get_children %}
{% with post=post.specific %}
<h2>{{ post.title }}</h2>
{{ post.intro }}
{{ post.body|richtext }}
{% endwith %}
{% endfor %}
2015-06-01 14:30:07 +00:00
Image support
~~~~~~~~~~~~~
2016-10-28 00:15:44 +00:00
Wagtail provides support for images out of the box. To add them to
your `` BlogPage `` model:
2015-06-01 14:30:07 +00:00
2015-10-05 09:01:03 +00:00
.. code-block :: python
2015-06-01 14:30:07 +00:00
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
class BlogPage(Page):
main_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
2016-03-22 06:41:19 +00:00
search_fields = Page.search_fields + [
2015-06-01 14:30:07 +00:00
index.SearchField('intro'),
index.SearchField('body'),
2016-03-22 06:41:19 +00:00
]
2015-06-01 14:30:07 +00:00
content_panels = Page.content_panels + [
FieldPanel('date'),
ImageChooserPanel('main_image'),
FieldPanel('intro'),
FieldPanel('body'),
]
2015-07-07 14:33:44 +00:00
Run `` python manage.py makemigrations `` and `` python manage.py migrate `` .
2015-06-21 22:39:47 +00:00
Adjust your blog page template to include the image:
2015-06-01 14:30:07 +00:00
2015-10-05 09:01:03 +00:00
.. code-block :: html+django
2015-06-01 14:30:07 +00:00
{% extends "base.html" %}
2015-07-07 14:33:44 +00:00
{% load wagtailcore_tags wagtailimages_tags %}
2015-06-01 14:30:07 +00:00
2015-08-03 13:35:32 +00:00
{% block body_class %}template-blogpage{% endblock %}
2015-06-01 14:30:07 +00:00
{% block content %}
2015-10-13 08:52:16 +00:00
<h1>{{ page.title }}</h1>
<p class="meta">{{ page.date }}</p>
2015-06-01 14:30:07 +00:00
2015-10-13 08:52:16 +00:00
{% if page.main_image %}
2016-04-28 00:23:39 +00:00
{% image page.main_image width-400 %}
2015-06-01 14:30:07 +00:00
{% endif %}
2015-10-13 08:52:16 +00:00
<div class="intro">{{ page.intro }}</div>
2015-06-01 14:30:07 +00:00
2015-10-13 08:52:16 +00:00
{{ page.body|richtext }}
2015-06-01 14:30:07 +00:00
{% endblock %}
.. figure :: ../_static/images/tutorial/tutorial_6.png
:alt: A blog post sample
You can read more about using images in templates in the
2016-01-16 13:19:05 +00:00
:doc: `docs <../topics/images>` .
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
Tagging Posts
2015-06-01 14:30:07 +00:00
~~~~~~~~~~~~~
2016-10-28 00:15:44 +00:00
Let's say we want to let editors "tag" their posts, so that readers can, e.g.,
view all bicycle-related content together. For this, we'll need to invoke
the tagging system bundled with Wagtail, attach it to the `` BlogPage ``
model and content panels, and render linked tags on the blog post template.
Of course, we'll need a working tag-specific URL view as well.
First, alter `` models.py `` once more:
2015-06-01 14:30:07 +00:00
2015-10-05 09:01:03 +00:00
.. code-block :: python
2015-06-01 14:30:07 +00:00
from django.db import models
2016-10-28 00:15:44 +00:00
from modelcluster.tags import ClusterTaggableManager
2015-06-01 14:30:07 +00:00
from modelcluster.fields import ParentalKey
2016-10-28 00:15:44 +00:00
from taggit.models import TaggedItemBase
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
from wagtail.wagtailcore.models import Page
2015-06-01 14:30:07 +00:00
from wagtail.wagtailcore.fields import RichTextField
2016-10-28 00:15:44 +00:00
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel
2015-06-01 14:30:07 +00:00
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
2016-10-28 00:15:44 +00:00
class BlogPageTag(TaggedItemBase):
content_object = ParentalKey('BlogPage', related_name='tagged_items')
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
class BlogPage(Page):
main_image = models.ForeignKey(
'wagtailimages.Image',
2015-06-01 14:30:07 +00:00
null=True,
blank=True,
2016-10-28 00:15:44 +00:00
on_delete=models.SET_NULL,
2015-06-01 14:30:07 +00:00
related_name='+'
)
2016-10-28 00:15:44 +00:00
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
content_panels = Page.content_panels + [
FieldPanel('date'),
ImageChooserPanel('main_image'),
FieldPanel('intro'),
FieldPanel('body'),
MultiFieldPanel([
FieldPanel('tags'),
], heading="Tags"),
2015-06-01 14:30:07 +00:00
]
2016-10-28 00:15:44 +00:00
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
Note the new `` modelcluster `` and `` taggit `` imports, the addition of a new
`` BlogPageTag `` model, the addition of a `` tags `` field on `` BlogPage `` ,
and the use of `` MultiFieldPanel `` in `` content_panels `` to let users
select tags.
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
Edit one of your `` BlogPage `` instances, and you should now be able to tag posts:
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
.. figure :: ../_static/images/tutorial/tutorial_8.png
:alt: Tagging a post
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
To render tags on a `` BlogPage `` , add this to `` blog_page.html: ``
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
.. code-block :: html+django
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
{% if page.specific.tags.all.count %}
<div class="tags">
<h3>Tags</h3>
{% for tag in page.specific.tags.all %}
<a href="{% slugurl 'tags' %}?tag={{ tag }}"><button type="button">{{ tag }}</button></a>
{% endfor %}
</div>
{% endif %}
Visiting a blog post with tags should now show a set of linked
buttons at the bottom - one for each tag. However, clicking a button
will get you a 404, since we haven't yet defined a "tags" view, which
is going to require a little extra magic. Add to `` models.py: ``
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
.. code-block :: python
2015-10-15 00:29:19 +00:00
2016-10-28 00:15:44 +00:00
class BlogTagIndexPage(Page):
2015-10-15 00:29:19 +00:00
2016-10-28 00:15:44 +00:00
def get_context(self, request):
# Filter by tag
tag = request.GET.get('tag')
blogpages = BlogPage.objects.filter().filter(tags__name=tag)
# Update template context
context = super(BlogTagIndexPage, self).get_context(request)
context['blogpages'] = blogpages
return context
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
Note that this Page-based model defines no fields of its own.
Even without fields, subclassing `` Page `` makes it a part of the
Wagtail ecosystem, so that you can give it a title and URL in the
admin, and so that you can manipulate its contents by returning
a queryset from its `` get_context() `` method.
Migrate this in, then create a new `` BlogTagIndexPage `` in the admin.
You'll probably want to create the new page/view under Homepage,
parallel to your Blog index. Give it the slug "tags" on the Promote tab.
Access `` /tags `` and Django will tell you what you probably already knew:
you need to create a template `` blog/blog_tag_index_page.html: ``
2015-06-01 14:30:07 +00:00
2015-10-05 09:01:03 +00:00
.. code-block :: html+django
2015-06-01 14:30:07 +00:00
{% extends "base.html" %}
2015-07-07 14:33:44 +00:00
{% load wagtailcore_tags %}
2015-06-01 14:30:07 +00:00
{% block content %}
2016-10-28 00:15:44 +00:00
{% if request.GET.tag|length %}
<h4>Showing pages tagged "{{ request.GET.tag|safe }}"</h4>
2015-06-01 14:30:07 +00:00
{% endif %}
2016-10-28 00:15:44 +00:00
{% for blogpage in blogpages %}
<p>
<strong><a href="{% pageurl blogpage %}">{{ blogpage.title }}</a></strong><br />
<small>Revised: {{ blogpage.latest_revision_created_at }}</small><br />
{% if blogpage.author %}
<p>By {{ blogpage.author.profile }}</p>
{% endif %}
</p>
{% empty %}
No pages found with that tag.
{% endfor %}
2015-06-01 14:30:07 +00:00
{% endblock %}
2016-10-28 00:15:44 +00:00
Unlike in the previous example, we're linking to pages here with the builtin `` pageurl ``
tag rather than `` slugurl `` . The difference is that `` pageurl `` takes a Page object
as an argument. Use whichever one best suits your purpose.
2015-06-01 14:30:07 +00:00
2016-10-28 00:15:44 +00:00
We're also calling the built-in `` latest_revision_created_at `` field on the `` Page ``
model - handy to know this is always available.
We haven't yet added an "author" field to our `` BlogPage `` model, nor do we have
a Profile model for authors - we'll leave those as an exercise for the reader.
Clicking the tag button at the bottom of a BlogPost should now render a page
something like this:
.. figure :: ../_static/images/tutorial/tutorial_9.png
:alt: A simple tag view
2015-06-01 14:30:07 +00:00
Where next
----------
- Read the Wagtail :doc: `topics <../topics/index>` and :doc: `reference <../reference/index>` documentation
- Learn how to implement :doc: `StreamField <../topics/streamfield>` for freeform page content
- Browse through the :doc: `advanced topics <../advanced_topics/index>` section and read :doc: `third-party tutorials <../advanced_topics/third_party_tutorials>`