tutorial.rst 20 KB

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