0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00
wagtail/docs/getting_started/tutorial.rst

431 lines
12 KiB
ReStructuredText
Raw Normal View History

2015-06-01 16:30:07 +02:00
Your first Wagtail site
=======================
.. 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`.
1. Install Wagtail and its dependencies::
2015-06-01 16:30:07 +02:00
pip install wagtail
2015-06-01 16:30:07 +02:00
2. Start your site::
2015-06-01 16:30:07 +02:00
wagtail start mysite
cd mysite
2015-06-01 16:30:07 +02: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 17:23:13 +02:00
"home" app with a blank ``HomePage`` model and basic templates and a sample
"search" app.
2015-06-01 16:30:07 +02:00
3. Install project dependencies::
2015-06-01 16:30:07 +02: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 16:30:07 +02:00
If you haven't updated the project settings, this will be a SQLite
database file in the project directory.
5. Create an admin user::
2015-06-01 16:30:07 +02:00
python manage.py createsuperuser
2015-06-01 16:30:07 +02:00
6. ``python manage.py runserver`` If everything worked,
2015-06-01 16:30:07 +02:00
http://127.0.0.1:8000 will show you a welcome page
.. figure:: ../_static/images/tutorial/tutorial_1.png
:alt: Wagtail welcome message
You can now access the administrative area at http://127.0.0.1:8000/admin
2015-06-01 16:30:07 +02:00
.. figure:: ../_static/images/tutorial/tutorial_2.png
:alt: Administrative screen
Extend the HomePage model
-------------------------
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 16:30:07 +02:00
Edit ``home/models.py`` as follows, to add a ``body`` field to the model:
2015-06-01 16:30:07 +02:00
.. code-block:: python
2015-06-01 16:30:07 +02:00
from __future__ import unicode_literals
2015-06-01 16:30:07 +02: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 + [
FieldPanel('body', classname="full")
2015-06-01 16:30:07 +02:00
]
``body`` is defined as ``RichTextField``, a special Wagtail field. You
2015-06-01 17:23:13 +02: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 18:28:49 +02:00
capabilities and the layout of the editing interface. :doc:`More on creating Page models. <../topics/pages>`
2015-06-01 16:30:07 +02: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 17:07:15 +02: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.
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 16:30:07 +02:00
.. code-block:: html+django
2015-06-01 16:30:07 +02:00
{% extends "base.html" %}
{% load wagtailcore_tags %}
2015-06-01 16:30:07 +02:00
2015-06-01 17:23:13 +02:00
{% block body_class %}template-homepage{% endblock %}
2015-06-01 16:30:07 +02:00
{% block content %}
{{ page.body|richtext }}
2015-06-01 16:30:07 +02:00
{% endblock %}
.. figure:: ../_static/images/tutorial/tutorial_3.png
:alt: Updated homepage
Wagtail template tags
~~~~~~~~~~~~~~~~~~~~~
Wagtail provides a number of :ref:`template tags & filters <template-tags-and-filters>`
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 %}
{{ page.body|richtext }}
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 16:30:07 +02:00
A basic blog
------------
2015-06-01 17:23:13 +02: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 16:30:07 +02:00
The following example defines a basic blog post model in ``blog/models.py``:
2015-06-01 16:30:07 +02:00
.. code-block:: python
2015-06-01 16:30:07 +02: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)
search_fields = Page.search_fields + [
2015-06-01 16:30:07 +02:00
index.SearchField('intro'),
index.SearchField('body'),
]
2015-06-01 16:30:07 +02:00
content_panels = Page.content_panels + [
FieldPanel('date'),
FieldPanel('intro'),
FieldPanel('body', classname="full")
]
.. 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'),
)
Create a template at ``blog/templates/blog/blog_page.html``:
.. code-block:: html+django
{% extends "base.html" %}
{% load wagtailcore_tags %}
2015-08-03 15:35:32 +02:00
{% block body_class %}template-blogpage{% endblock %}
{% block content %}
<h1>{{ page.title }}</h1>
<p class="meta">{{ page.date }}</p>
<div class="intro">{{ page.intro }}</div>
{{ page.body|richtext }}
{% endblock %}
2015-06-01 17:23:13 +02:00
Run ``python manage.py makemigrations`` and ``python manage.py migrate``.
2015-06-01 16:30:07 +02:00
.. figure:: ../_static/images/tutorial/tutorial_4.png
:alt: Create page screen
.. figure:: ../_static/images/tutorial/tutorial_5.png
:alt: Page edit screen
Image support
~~~~~~~~~~~~~
Wagtail provides support for images out of the box. To add them to your
model:
.. code-block:: python
2015-06-01 16:30:07 +02: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)
search_fields = Page.search_fields + [
2015-06-01 16:30:07 +02:00
index.SearchField('intro'),
index.SearchField('body'),
]
2015-06-01 16:30:07 +02:00
content_panels = Page.content_panels + [
FieldPanel('date'),
ImageChooserPanel('main_image'),
FieldPanel('intro'),
FieldPanel('body'),
]
Run ``python manage.py makemigrations`` and ``python manage.py migrate``.
Adjust your blog page template to include the image:
2015-06-01 16:30:07 +02:00
.. code-block:: html+django
2015-06-01 16:30:07 +02:00
{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}
2015-06-01 16:30:07 +02:00
2015-08-03 15:35:32 +02:00
{% block body_class %}template-blogpage{% endblock %}
2015-06-01 16:30:07 +02:00
{% block content %}
<h1>{{ page.title }}</h1>
<p class="meta">{{ page.date }}</p>
2015-06-01 16:30:07 +02:00
{% if page.main_image %}
{% image page.main_image width-400 %}
2015-06-01 16:30:07 +02:00
{% endif %}
<div class="intro">{{ page.intro }}</div>
2015-06-01 16:30:07 +02:00
{{ page.body|richtext }}
2015-06-01 16:30:07 +02: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
:doc:`docs <../topics/images>`.
2015-06-01 16:30:07 +02:00
Blog Index
~~~~~~~~~~
Let us extend the Blog app to provide an index.
.. code-block:: python
2015-06-01 16:30:07 +02:00
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
The above creates an index type to collect all our blog posts.
``blog/templates/blog/blog_index_page.html``
.. code-block:: html+django
2015-06-01 16:30:07 +02:00
{% extends "base.html" %}
{% load wagtailcore_tags %}
2015-06-01 16:30:07 +02:00
2015-06-02 17:03:57 +02:00
{% block body_class %}template-blogindexpage{% endblock %}
2015-06-01 16:30:07 +02:00
{% block content %}
<h1>{{ page.title }}</h1>
2015-06-01 16:30:07 +02:00
<div class="intro">{{ page.intro|richtext }}</div>
2015-06-01 16:30:07 +02:00
{% endblock %}
Related items
~~~~~~~~~~~~~
Let's extend the BlogIndexPage to add related links. The related links
can be BlogPages or external links. Change ``blog/models.py`` to
.. code-block:: python
2015-06-01 16:30:07 +02:00
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import (FieldPanel,
InlinePanel,
MultiFieldPanel,
PageChooserPanel)
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
2015-06-02 17:03:57 +02:00
# ...
2015-06-01 16:30:07 +02:00
class LinkFields(models.Model):
link_external = models.URLField("External link", blank=True)
link_page = models.ForeignKey(
'wagtailcore.Page',
null=True,
blank=True,
related_name='+'
)
@property
def link(self):
if self.link_page:
return self.link_page.url
else:
return self.link_external
panels = [
FieldPanel('link_external'),
PageChooserPanel('link_page'),
]
class Meta:
abstract = True
# Related links
class RelatedLink(LinkFields):
title = models.CharField(max_length=255, help_text="Link title")
panels = [
FieldPanel('title'),
MultiFieldPanel(LinkFields.panels, "Link"),
]
class Meta:
abstract = True
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full"),
2015-06-01 16:30:07 +02:00
InlinePanel('related_links', label="Related links"),
]
class BlogIndexRelatedLink(Orderable, RelatedLink):
page = ParentalKey('BlogIndexPage', related_name='related_links')
2015-06-01 16:30:07 +02:00
.. figure:: ../_static/images/tutorial/tutorial_7.png
:alt: Blog index edit screen
Extend ``blog_index_page.html`` to show related items
.. code-block:: html+django
2015-06-01 16:30:07 +02:00
{% extends "base.html" %}
{% load wagtailcore_tags %}
2015-06-01 16:30:07 +02:00
2015-06-02 17:06:03 +02:00
{% block body_class %}template-blogindexpage{% endblock %}
2015-06-01 16:30:07 +02:00
{% block content %}
<h1>{{ page.title }}</h1>
2015-06-01 16:30:07 +02:00
<div class="intro">{{ page.intro|richtext }}</div>
2015-06-01 16:30:07 +02:00
{% if page.related_links.all %}
2015-06-01 16:30:07 +02:00
<ul>
{% for item in page.related_links.all %}
2015-06-01 16:30:07 +02:00
<li><a href="{{ item.link }}">{{ item.title }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
You now have a fully working blog with featured blog posts.
.. figure:: ../_static/images/tutorial/tutorial_8.png
:alt: Barebones blog index
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>`