On this page
Writing templates
Wagtail uses Django’s templating language. For developers new to Django, start with Django’s own template documentation: Templates
Python programmers new to Django/Wagtail may prefer more technical documentation: The Django template language: for Python programmers
You should be familiar with Django templating basics before continuing with this documentation.
Templates
Every type of page or “content type” in Wagtail is defined as a “model” in a file called models.py
. If your site has a blog, you might have a BlogPage
model and another called BlogPageListing
. The names of the models are up to the Django developer.
For each page model in models.py
, Wagtail assumes an HTML template file exists of (almost) the same name. The Front End developer may need to create these templates themselves by referring to models.py
to infer template names from the models defined therein.
To find a suitable template, Wagtail converts CamelCase names to snake_case. So for a BlogPage
, a template blog_page.html
will be expected. The name of the template file can be overridden per model if necessary.
Template files are assumed to exist here:
name_of_project/
name_of_app/
templates/
name_of_app/
blog_page.html
models.py
For more information, see the Django documentation for the application directories template loader.
Page content
The data/content entered into each page is accessed/output through Django’s {{ double-brace }}
notation. Each field from the model must be accessed by prefixing page.
. e.g the page title {{ page.title }}
or another field {{ page.author }}
.
A custom variable name can be configured on the page model
. If a custom name is defined, page
is still available for use in shared templates.
Additionally request.
is available and contains Django’s request object.
Static assets
Static files e.g CSS, JS and images are typically stored here:
name_of_project/
name_of_app/
static/
name_of_app/
css/
js/
images/
models.py
(The names “css”, “js” etc aren’t important, only their position within the tree.)
Any file within the static folder should be inserted into your HTML using the {% static %}
tag. More about it: Static files (tag).
User images
Images uploaded to a Wagtail site by its users (as opposed to a developer’s static files, mentioned above) go into the image library and from there are added to pages via the page editor interface.
Unlike other CMSs, adding images to a page does not involve choosing a “version” of the image to use. Wagtail has no predefined image “formats” or “sizes”. Instead the template developer defines image manipulation to occur on the fly when the image is requested, via a special syntax within the template.
Images from the library must be requested using this syntax, but a developer’s static images can be added via conventional means e.g img
tags. Only images from the library can be manipulated on the fly.
Read more about the image manipulation syntax here How to use images in templates.
Wagtail User Bar
This tag provides a contextual flyout menu for logged-in users. The menu gives editors the ability to edit the current page or add a child page, besides the options to show the page in the Wagtail page explorer or jump to the Wagtail admin dashboard. Moderators are also given the ability to accept or reject a page being previewed as part of content moderation.
This tag may be used on standard Django views, without page object. The user bar will contain one item pointing to the admin.
We recommend putting the tag near the top of the <body>
element so keyboard users can reach it. You should consider putting the tag after any skip links but before the navigation and main content of your page.
{% load wagtailuserbar %}
...
<body>
<a id="#content">Skip to content</a>
{% wagtailuserbar %} {# This is a good place for the userbar #}
<nav>
...
</nav>
<main id="content">
...
</main>
</body>
By default the User Bar appears in the bottom right of the browser window, inset from the edge. If this conflicts with your design it can be moved by passing a parameter to the template tag. These examples show you how to position the userbar in each corner of the screen:
...
{% wagtailuserbar 'top-left' %}
{% wagtailuserbar 'top-right' %}
{% wagtailuserbar 'bottom-left' %}
{% wagtailuserbar 'bottom-right' %}
...
The userbar can be positioned where it works best with your design. Alternatively, you can position it with a CSS rule in your own CSS files, for example:
.wagtail-userbar {
top: 200px !important;
left: 10px !important;
}
Varying output between preview and live
Sometimes you may wish to vary the template output depending on whether the page is being previewed or viewed live. For example, if you have visitor tracking code such as Google Analytics in place on your site, it’s a good idea to leave this out when previewing, so that editor activity doesn’t appear in your analytics reports. Wagtail provides a request.is_preview
variable to distinguish between preview and live:
{% if not request.is_preview %}
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
...
</script>
{% endif %}
If the page is being previewed, request.preview_mode
can be used to determine the specific preview mode being used, if the page supports multiple preview modes
.
© 2014-present Torchbox Ltd and individual contributors.
All rights are reserved.
Licensed under the BSD License.
https://docs.wagtail.org/en/v3.0.3/topics/writing_templates.html