tutorial.rst 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. Your first Wagtail site
  2. =======================
  3. .. note::
  4. 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`.
  5. 1. Install Wagtail and its dependencies:
  6. .. code-block:: console
  7. $ pip install wagtail
  8. 2. Start your site:
  9. .. code-block:: console
  10. $ wagtail start mysite
  11. $ cd mysite
  12. Wagtail provides a ``start`` command similar to
  13. ``django-admin.py startproject``. Running ``wagtail start mysite`` in
  14. your project will generate a new ``mysite`` folder with a few
  15. Wagtail-specific extras, including the required project settings, a
  16. "home" app with a blank ``HomePage`` model and basic templates and a sample
  17. "search" app.
  18. 3. Install project dependencies:
  19. .. code-block:: console
  20. $ pip install -r requirements.txt
  21. This ensures that you have the relevant version of Django for the project you've just created.
  22. 4. Create the database:
  23. .. code-block:: console
  24. $ python manage.py migrate
  25. If you haven't updated the project settings, this will be a SQLite
  26. database file in the project directory.
  27. 5. Create an admin user:
  28. .. code-block:: console
  29. $ python manage.py createsuperuser
  30. 6. ``python manage.py runserver`` If everything worked,
  31. http://127.0.0.1:8000 will show you a welcome page
  32. .. figure:: ../_static/images/tutorial/tutorial_1.png
  33. :alt: Wagtail welcome message
  34. You can now access the administrative area at http://127.0.0.1:8000/admin
  35. .. figure:: ../_static/images/tutorial/tutorial_2.png
  36. :alt: Administrative screen
  37. Extend the HomePage model
  38. -------------------------
  39. 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.
  40. Edit ``home/models.py`` as follows, to add a ``body`` field to the model:
  41. .. code-block:: python
  42. from __future__ import unicode_literals
  43. from django.db import models
  44. from wagtail.wagtailcore.models import Page
  45. from wagtail.wagtailcore.fields import RichTextField
  46. from wagtail.wagtailadmin.edit_handlers import FieldPanel
  47. class HomePage(Page):
  48. body = RichTextField(blank=True)
  49. content_panels = Page.content_panels + [
  50. FieldPanel('body', classname="full"),
  51. ]
  52. ``body`` is defined as ``RichTextField``, a special Wagtail field. You
  53. can use any of the `Django core fields <https://docs.djangoproject.com/en/1.8/ref/models/fields/>`__. ``content_panels`` define the
  54. capabilities and the layout of the editing interface. :doc:`More on creating Page models. <../topics/pages>`
  55. Run ``python manage.py makemigrations``, then
  56. ``python manage.py migrate`` to update the database with your model
  57. changes. You must run the above commands each time you make changes to
  58. the model definition.
  59. 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.
  60. The page template now needs to be updated to reflect the changes made
  61. to the model. Wagtail uses normal Django templates to render each page
  62. type. It automatically generates a template filename from the model name
  63. by separating capital letters with underscores (e.g. HomePage becomes
  64. home\_page.html). Edit
  65. ``home/templates/home/home_page.html`` to contain the following:
  66. .. code-block:: html+django
  67. {% extends "base.html" %}
  68. {% load wagtailcore_tags %}
  69. {% block body_class %}template-homepage{% endblock %}
  70. {% block content %}
  71. {{ page.body|richtext }}
  72. {% endblock %}
  73. .. figure:: ../_static/images/tutorial/tutorial_3.png
  74. :alt: Updated homepage
  75. Wagtail template tags
  76. ~~~~~~~~~~~~~~~~~~~~~
  77. Wagtail provides a number of :ref:`template tags & filters <template-tags-and-filters>`
  78. which can be loaded by including ``{% load wagtailcore_tags %}`` at the top of
  79. your template file.
  80. In this tutorial, we use the `richtext` filter to escape and print the contents
  81. of a ``RichTextField``:
  82. .. code-block:: html+django
  83. {% load wagtailcore_tags %}
  84. {{ page.body|richtext }}
  85. Produces:
  86. .. code-block:: html
  87. <div class="rich-text">
  88. <p>
  89. <b>Welcome</b> to our new site!
  90. </p>
  91. </div>
  92. **Note:** You'll need to include ``{% load wagtailcore_tags %}`` in each
  93. template that uses Wagtail's tags. Django will throw a ``TemplateSyntaxError``
  94. if the tags aren't loaded.
  95. A basic blog
  96. ------------
  97. We are now ready to create a blog. To do so, run
  98. ``python manage.py startapp blog`` to create a new app in your Wagtail site.
  99. Add the new ``blog`` app to ``INSTALLED_APPS`` in ``mysite/settings/base.py``.
  100. Blog Index and Posts
  101. ~~~~~~~~~~~~~~~~~~~~
  102. Lets start with a simple index page for our blog. In ``blog/models.py``:
  103. .. code-block:: python
  104. from wagtail.wagtailcore.models import Page
  105. from wagtail.wagtailcore.fields import RichTextField
  106. from wagtail.wagtailadmin.edit_handlers import FieldPanel
  107. class BlogIndexPage(Page):
  108. intro = RichTextField(blank=True)
  109. content_panels = Page.content_panels + [
  110. FieldPanel('intro', classname="full")
  111. ]
  112. Run ``python manage.py makemigrations`` and ``python manage.py migrate``.
  113. Since the model is called ``BlogIndexPage``, the default template name
  114. (unless we override it) will be ``blog/templates/blog/blog_index_page.html:``
  115. .. code-block:: html+django
  116. {% extends "base.html" %}
  117. {% load wagtailcore_tags %}
  118. {% block body_class %}template-blogindexpage{% endblock %}
  119. {% block content %}
  120. <h1>{{ page.title }}</h1>
  121. <div class="intro">{{ page.intro|richtext }}</div>
  122. {% for post in page.get_children %}
  123. <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
  124. {{ post.specific.intro }}
  125. {{ post.specific.body|richtext }}
  126. {% endfor %}
  127. {% endblock %}
  128. Most of this should be familiar, but we'll explain ``get_children`` a bit later.
  129. Note the ``pageurl`` tag, which is similar to Django's ``url`` tag but
  130. takes a Wagtail Page object as an argument.
  131. In the Wagtail admin, create a ``BlogIndexPage`` under the Homepage,
  132. make sure it has the slug "blog" on the Promote tab, and publish it.
  133. You should now be able to access the url ``/blog`` on your site
  134. (note how the slug from the Promote tab defines the page URL).
  135. Now we need a model and template for our blog posts. In ``blog/models.py``:
  136. .. code-block:: python
  137. from django.db import models
  138. from wagtail.wagtailcore.models import Page
  139. from wagtail.wagtailcore.fields import RichTextField
  140. from wagtail.wagtailadmin.edit_handlers import FieldPanel
  141. from wagtail.wagtailsearch import index
  142. # ...
  143. class BlogPage(Page):
  144. date = models.DateField("Post date")
  145. intro = models.CharField(max_length=250)
  146. body = RichTextField(blank=True)
  147. search_fields = Page.search_fields + [
  148. index.SearchField('intro'),
  149. index.SearchField('body'),
  150. ]
  151. content_panels = Page.content_panels + [
  152. FieldPanel('date'),
  153. FieldPanel('intro'),
  154. FieldPanel('body', classname="full"),
  155. ]
  156. Run ``python manage.py makemigrations`` and ``python manage.py migrate``.
  157. Create a template at ``blog/templates/blog/blog_page.html``:
  158. .. code-block:: html+django
  159. {% extends "base.html" %}
  160. {% load wagtailcore_tags %}
  161. {% block body_class %}template-blogpage{% endblock %}
  162. {% block content %}
  163. <h1>{{ page.title }}</h1>
  164. <p class="meta">{{ page.date }}</p>
  165. <div class="intro">{{ page.intro }}</div>
  166. {{ page.body|richtext }}
  167. <p><a href="{{ page.get_parent.url }}">Return to blog</a></p>
  168. {% endblock %}
  169. Note the use of Wagtail's built-in ``get_parent()`` method to obtain the
  170. URL of the blog this post is a part of.
  171. Now create a few blog posts as children of ``BlogIndexPage.``
  172. Be sure to select type "Blog Page" when creating your posts.
  173. .. figure:: ../_static/images/tutorial/tutorial_4a.png
  174. :alt: Create blog post as child of BlogIndex
  175. .. figure:: ../_static/images/tutorial/tutorial_4b.png
  176. :alt: Choose type BlogPost
  177. Wagtail gives you full control over what kinds of content can be created under
  178. various parent content types. By default, any page type can be a child of any
  179. other page type.
  180. .. figure:: ../_static/images/tutorial/tutorial_5.png
  181. :alt: Page edit screen
  182. You should now have the very beginnings of a working blog.
  183. Access the ``/blog`` URL and you should see something like this:
  184. .. figure:: ../_static/images/tutorial/tutorial_7.png
  185. :alt: Blog basics
  186. Titles should link to post pages, and a link back to the blog's
  187. homepage should appear in the footer of each post page.
  188. Parents and Children
  189. ~~~~~~~~~~~~~~~~~~~~
  190. Much of the work you'll be doing in Wagtail revolves around the concept of hierarchical
  191. "tree" structures consisting of nodes and leaves (see :doc:`../reference/pages/theory`).
  192. In this case, the ``BlogIndexPage`` is a "node" and individual ``BlogPage`` instances
  193. are the "leaves".
  194. Take another look at the guts of ``BlogIndexPage:``
  195. .. code-block:: html+django
  196. {% for post in page.get_children %}
  197. <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
  198. {{ post.specific.intro }}
  199. {{ post.specific.body|richtext }}
  200. {% endfor %}
  201. Every "page" in Wagtail can call out to its parent or children
  202. from its own position in the hierarchy. But why do we have to
  203. specify ``post.specific.intro`` rather than ``post.intro``?
  204. This has to do with the way we defined our model:
  205. ``class BlogPage(Page):``
  206. The ``get_children()`` method gets us a list of instances of the ``Page`` base class.
  207. When we want to reference properties of the instances that inherit from the base class,
  208. Wagtail provides the ``specific`` method that retrieves the actual ``BlogPage`` record.
  209. While the "title" field is present on the base ``Page`` model, "intro" is only present
  210. on the ``BlogPage`` model, so we need ``.specific`` to access it.
  211. To tighten up template code like this, we could use Django's ``with`` tag:
  212. .. code-block:: html+django
  213. {% for post in page.get_children %}
  214. {% with post=post.specific %}
  215. <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
  216. <p>{{ post.intro }}</p>
  217. {{ post.body|richtext }}
  218. {% endwith %}
  219. {% endfor %}
  220. When you start writing more customized Wagtail code, you'll find a whole set of QuerySet
  221. modifiers to help you navigate the hierarchy.
  222. .. code-block:: python
  223. # Given a page object 'somepage':
  224. MyModel.objects.descendant_of(somepage)
  225. child_of(page) / not_child_of(somepage)
  226. ancestor_of(somepage) / not_ancestor_of(somepage)
  227. parent_of(somepage) / not_parent_of(somepage)
  228. sibling_of(somepage) / not_sibling_of(somepage)
  229. # ... and ...
  230. somepage.get_children()
  231. somepage.get_ancestors()
  232. somepage.get_descendants()
  233. somepage.get_siblings()
  234. For more information, see: :doc:`../reference/pages/queryset_reference`
  235. Overriding Context
  236. ~~~~~~~~~~~~~~~~~~
  237. There are a couple of problems with our blog index view:
  238. 1) Blogs generally display content in *reverse* chronological order
  239. 2) We want to make sure we're only displaying *published* content.
  240. To accomplish these things, we need to do more than just grab the index
  241. page's children in the template. Instead, we'll want to modify the
  242. QuerySet in the model definition. Wagtail makes this possible via
  243. the overridable ``get_context()`` method. Modify your ``BlogIndexPage``
  244. model like this:
  245. .. code-block:: python
  246. class BlogIndexPage(Page):
  247. intro = RichTextField(blank=True)
  248. def get_context(self, request):
  249. # Update context to include only published posts, ordered by reverse-chron
  250. context = super(BlogIndexPage, self).get_context(request)
  251. blogpages = self.get_children().live().order_by('-first_published_at')
  252. context['blogpages'] = blogpages
  253. return context
  254. All we've done here is retrieve the original context, create a custom queryset,
  255. add it to the retrieved context, and return the modified context back to the view.
  256. You'll also need to modify your ``blog_index_page.html`` template slightly.
  257. Change:
  258. ``{% for post in page.get_children %}`` to ``{% for post in blogpages %}``
  259. Now try unpublishing one of your posts - it should disappear from the blog index
  260. page. The remaining posts should now be sorted with the most recently modified
  261. posts first.
  262. Images
  263. ~~~~~~
  264. Let's add the ability to attach an image gallery to our blog posts. While it's possible to simply insert images into the ``body`` rich text field, there are several advantages to setting up our gallery images as a new dedicated object type within the database - this way, you have full control of the layout and styling of the images on the template, rather than having to lay them out in a particular way within the rich text field. It also makes it possible for the images to be used elsewhere, independently of the blog text - for example, displaying a thumbnail on the blog index page.
  265. Add a new ``BlogPageGalleryImage`` model to ``models.py``:
  266. .. code-block:: python
  267. from django.db import models
  268. from modelcluster.fields import ParentalKey
  269. from wagtail.wagtailcore.models import Page, Orderable
  270. from wagtail.wagtailcore.fields import RichTextField
  271. from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
  272. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  273. from wagtail.wagtailsearch import index
  274. # ...
  275. class BlogPage(Page):
  276. date = models.DateField("Post date")
  277. intro = models.CharField(max_length=250)
  278. body = RichTextField(blank=True)
  279. search_fields = Page.search_fields + [
  280. index.SearchField('intro'),
  281. index.SearchField('body'),
  282. ]
  283. content_panels = Page.content_panels + [
  284. FieldPanel('date'),
  285. FieldPanel('intro'),
  286. FieldPanel('body', classname="full"),
  287. InlinePanel('gallery_images', label="Gallery images"),
  288. ]
  289. class BlogPageGalleryImage(Orderable):
  290. page = ParentalKey(BlogPage, related_name='gallery_images')
  291. image = models.ForeignKey(
  292. 'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
  293. )
  294. caption = models.CharField(blank=True, max_length=250)
  295. panels = [
  296. ImageChooserPanel('image'),
  297. FieldPanel('caption'),
  298. ]
  299. Run ``python manage.py makemigrations`` and ``python manage.py migrate``.
  300. There are a few new concepts here, so let's take them one at a time:
  301. Inheriting from ``Orderable`` adds a ``sort_order`` field to the model, to keep track of the ordering of images in the gallery.
  302. The ``ParentalKey`` to ``BlogPage`` is what attaches the gallery images to a specific page. A ``ParentalKey`` works similarly to a ``ForeignKey``, but also defines ``BlogPageGalleryImage`` as a "child" of the ``BlogPage`` model, so that it's treated as a fundamental part of the page in operations like submitting for moderation, and tracking revision history.
  303. ``image`` is a ``ForeignKey`` to Wagtail's built-in ``Image`` model, where the images themselves are stored. This comes with a dedicated panel type, ``ImageChooserPanel``, which provides a pop-up interface for choosing an existing image or uploading a new one. This way, we allow an image to exist in multiple galleries - effectively, we've created a many-to-many relationship between pages and images.
  304. Specifying ``on_delete=models.CASCADE`` on the foreign key means that if the image is deleted from the system, the gallery entry is deleted as well. (In other situations, it might be appropriate to leave the entry in place - for example, if an "our staff" page included a list of people with headshots, and one of those photos was deleted, we'd rather leave the person in place on the page without a photo. In this case, we'd set the foreign key to ``blank=True, null=True, on_delete=models.SET_NULL``.)
  305. Finally, adding the ``InlinePanel`` to ``BlogPage.content_panels`` makes the gallery images available on the editing interface for ``BlogPage``.
  306. Adjust your blog page template to include the images:
  307. .. code-block:: html+django
  308. {% extends "base.html" %}
  309. {% load wagtailcore_tags wagtailimages_tags %}
  310. {% block body_class %}template-blogpage{% endblock %}
  311. {% block content %}
  312. <h1>{{ page.title }}</h1>
  313. <p class="meta">{{ page.date }}</p>
  314. <div class="intro">{{ page.intro }}</div>
  315. {{ page.body|richtext }}
  316. {% for item in page.gallery_images.all %}
  317. <div style="float: left; margin: 10px">
  318. {% image item.image fill-320x240 %}
  319. <p>{{ item.caption }}</p>
  320. </div>
  321. {% endfor %}
  322. <p><a href="{{ page.get_parent.url }}">Return to blog</a></p>
  323. {% endblock %}
  324. Here we use the ``{% image %}`` tag (which exists in the ``wagtailimages_tags`` library, imported at the top of the template) to insert an ``<img>`` element, with a ``fill-320x240`` parameter to indicate that the image should be resized and cropped to fill a 320x240 rectangle. You can read more about using images in templates in the :doc:`docs <../topics/images>`.
  325. .. figure:: ../_static/images/tutorial/tutorial_6.jpg
  326. :alt: A blog post sample
  327. Since our gallery images are database objects in their own right, we can now query and re-use them independently of the blog post body. Let's define a ``main_image`` method, which returns the image from the first gallery item (or ``None`` if no gallery items exist):
  328. .. code-block:: python
  329. class BlogPage(Page):
  330. date = models.DateField("Post date")
  331. intro = models.CharField(max_length=250)
  332. body = RichTextField(blank=True)
  333. def main_image(self):
  334. gallery_item = self.gallery_images.first()
  335. if gallery_item:
  336. return gallery_item.image
  337. else:
  338. return None
  339. search_fields = Page.search_fields + [
  340. index.SearchField('intro'),
  341. index.SearchField('body'),
  342. ]
  343. content_panels = Page.content_panels + [
  344. FieldPanel('date'),
  345. FieldPanel('intro'),
  346. FieldPanel('body', classname="full"),
  347. InlinePanel('gallery_images', label="Gallery images"),
  348. ]
  349. This method is now available from our templates. Update ``blog_index_page.html`` to include the main image as a thumbnail alongside each post:
  350. .. code-block:: html+django
  351. {% load wagtailcore_tags wagtailimages_tags %}
  352. ...
  353. {% for post in page.get_children %}
  354. {% with post=post.specific %}
  355. <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
  356. {% with post.main_image as main_image %}
  357. {% if main_image %}{% image main_image fill-160x100 %}{% endif %}
  358. {% endwith %}
  359. <p>{{ post.intro }}</p>
  360. {{ post.body|richtext }}
  361. {% endwith %}
  362. {% endfor %}
  363. Tagging Posts
  364. ~~~~~~~~~~~~~
  365. Let's say we want to let editors "tag" their posts, so that readers can, e.g.,
  366. view all bicycle-related content together. For this, we'll need to invoke
  367. the tagging system bundled with Wagtail, attach it to the ``BlogPage``
  368. model and content panels, and render linked tags on the blog post template.
  369. Of course, we'll need a working tag-specific URL view as well.
  370. First, alter ``models.py`` once more:
  371. .. code-block:: python
  372. from django.db import models
  373. from modelcluster.fields import ParentalKey
  374. from modelcluster.tags import ClusterTaggableManager
  375. from taggit.models import TaggedItemBase
  376. from wagtail.wagtailcore.models import Page, Orderable
  377. from wagtail.wagtailcore.fields import RichTextField
  378. from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel
  379. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  380. from wagtail.wagtailsearch import index
  381. class BlogPageTag(TaggedItemBase):
  382. content_object = ParentalKey('BlogPage', related_name='tagged_items')
  383. class BlogPage(Page):
  384. date = models.DateField("Post date")
  385. intro = models.CharField(max_length=250)
  386. body = RichTextField(blank=True)
  387. tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
  388. # ...
  389. content_panels = Page.content_panels + [
  390. MultiFieldPanel([
  391. FieldPanel('date'),
  392. FieldPanel('tags'),
  393. ], heading="Blog information"),
  394. FieldPanel('intro'),
  395. FieldPanel('body'),
  396. InlinePanel('gallery_images', label="Gallery images"),
  397. ]
  398. Run ``python manage.py makemigrations`` and ``python manage.py migrate``.
  399. Note the new ``modelcluster`` and ``taggit`` imports, the addition of a new
  400. ``BlogPageTag`` model, and the addition of a ``tags`` field on ``BlogPage``.
  401. We've also taken the opportunity to use a ``MultiFieldPanel`` in ``content_panels``
  402. to group the date and tags fields together for readability.
  403. Edit one of your ``BlogPage`` instances, and you should now be able to tag posts:
  404. .. figure:: ../_static/images/tutorial/tutorial_8.png
  405. :alt: Tagging a post
  406. To render tags on a ``BlogPage``, add this to ``blog_page.html``:
  407. .. code-block:: html+django
  408. {% if page.tags.all.count %}
  409. <div class="tags">
  410. <h3>Tags</h3>
  411. {% for tag in page.tags.all %}
  412. <a href="{% slugurl 'tags' %}?tag={{ tag }}"><button type="button">{{ tag }}</button></a>
  413. {% endfor %}
  414. </div>
  415. {% endif %}
  416. Notice that we're linking to pages here with the builtin ``slugurl``
  417. tag rather than ``pageurl``, which we used earlier. The difference is that ``slugurl`` takes a
  418. Page slug (from the Promote tab) as an argument. ``pageurl`` is more commonly used because it
  419. is unambiguous and avoids extra database lookups. But in the case of this loop, the Page object
  420. isn't readily available, so we fall back on the less-preferred ``slugurl`` tag.
  421. Visiting a blog post with tags should now show a set of linked
  422. buttons at the bottom - one for each tag. However, clicking a button
  423. will get you a 404, since we haven't yet defined a "tags" view. Add to ``models.py``:
  424. .. code-block:: python
  425. class BlogTagIndexPage(Page):
  426. def get_context(self, request):
  427. # Filter by tag
  428. tag = request.GET.get('tag')
  429. blogpages = BlogPage.objects.filter().filter(tags__name=tag)
  430. # Update template context
  431. context = super(BlogTagIndexPage, self).get_context(request)
  432. context['blogpages'] = blogpages
  433. return context
  434. Note that this Page-based model defines no fields of its own.
  435. Even without fields, subclassing ``Page`` makes it a part of the
  436. Wagtail ecosystem, so that you can give it a title and URL in the
  437. admin, and so that you can manipulate its contents by returning
  438. a queryset from its ``get_context()`` method.
  439. Migrate this in, then create a new ``BlogTagIndexPage`` in the admin.
  440. You'll probably want to create the new page/view under Homepage,
  441. parallel to your Blog index. Give it the slug "tags" on the Promote tab.
  442. Access ``/tags`` and Django will tell you what you probably already knew:
  443. you need to create a template ``blog/blog_tag_index_page.html``:
  444. .. code-block:: html+django
  445. {% extends "base.html" %}
  446. {% load wagtailcore_tags %}
  447. {% block content %}
  448. {% if request.GET.tag|length %}
  449. <h4>Showing pages tagged "{{ request.GET.tag }}"</h4>
  450. {% endif %}
  451. {% for blogpage in blogpages %}
  452. <p>
  453. <strong><a href="{% pageurl blogpage %}">{{ blogpage.title }}</a></strong><br />
  454. <small>Revised: {{ blogpage.latest_revision_created_at }}</small><br />
  455. {% if blogpage.author %}
  456. <p>By {{ blogpage.author.profile }}</p>
  457. {% endif %}
  458. </p>
  459. {% empty %}
  460. No pages found with that tag.
  461. {% endfor %}
  462. {% endblock %}
  463. We're calling the built-in ``latest_revision_created_at`` field on the ``Page``
  464. model - handy to know this is always available.
  465. We haven't yet added an "author" field to our ``BlogPage`` model, nor do we have
  466. a Profile model for authors - we'll leave those as an exercise for the reader.
  467. Clicking the tag button at the bottom of a BlogPost should now render a page
  468. something like this:
  469. .. figure:: ../_static/images/tutorial/tutorial_9.png
  470. :alt: A simple tag view
  471. Where next
  472. ----------
  473. - Read the Wagtail :doc:`topics <../topics/index>` and :doc:`reference <../reference/index>` documentation
  474. - Learn how to implement :doc:`StreamField <../topics/streamfield>` for freeform page content
  475. - Browse through the :doc:`advanced topics <../advanced_topics/index>` section and read :doc:`third-party tutorials <../advanced_topics/third_party_tutorials>`