Created: 2018-10-06 Sat 00:38
$ pip install wagtail $ wagtail start mysite $ cd mysite $ pip install -r requirements.txt $ python manage.py migrate $ python manage.py createsuperuser $ python manage.py runserver
$ cat requirements.txt Django>=2.0,<2.1 wagtail>=2.2,<2.3 $
# models.py from django.db import models class Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) website = models.URLField()
# models.py from wagtail.core.models import Page class Publisher(Page): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) website = models.URLField()
# models.py from wagtail.core.models import Page class Publisher(Page): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) website = models.URLField() content_panels = Page.content_panels + [ FieldPanel('name'), FieldPanel('address'), FieldPanel('city'), FieldPanel('website'), ]
# project/name_of_app/templates/name_of_app/post_page.html {% extends "home/base.html" %} {% load static wagtailcore_tags wagtailimages_tags %} {% block title %}{{ self.title }} {% endblock title %} {% block meta_title %}{{ self.title }}{% endblock meta_title %} {% block meta_description %} {{ self.body|striptags|truncatewords:20 }} {% endblock meta_description %} {% block content %} {% if post.header_image %} {% image post.header_image original as header_image %} <img src="{{ header_image.url }}" class="img-responsive" ></img> <hr>
# models.py class PostPage(Page): body = RichTextField()
# models.py class PostPage(Page): body = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('quotation', blocks.StructBlock([ ('text', blocks.TextBlock()), ('author', blocks.CharBlock()), ])), ('video', EmbedBlock()), ])
# settings.py INSTALLED_APPS = [ ... 'wagtail.api.v2', ... ]
# api.py from wagtail.api.v2.endpoints import PagesAPIEndpoint from wagtail.api.v2.router import WagtailAPIRouter from wagtail.images.api.v2.endpoints import ImagesAPIEndpoint from wagtail.documents.api.v2.endpoints import DocumentsAPIEndpoint # Create the router. "wagtailapi" is the URL namespace api_router = WagtailAPIRouter('wagtailapi') api_router.register_endpoint('pages', PagesAPIEndpoint) api_router.register_endpoint('images', ImagesAPIEndpoint) api_router.register_endpoint('documents', DocumentsAPIEndpoint)
# urls.py from .api import api_router urlpatterns = [ ... url(r'^api/v2/', api_router.urls), ... # Ensure that the api_router line appears above # the default Wagtail page serving route url(r'', include(wagtail_urls)), ]
# models.py class StreamPage(Page): date = models.DateField("Post date") body = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ]) # Export fields over the API api_fields = [ APIField('date'), APIField('body'), ]
GET /api/v2/pages/5/ { "id": 5, "meta": { "type": "home.StreamPage", "detail_url": "http://localhost/api/v2/pages/5/", "html_url": "http://localhost/stream-page-title/", "slug": "stream-page-title", "show_in_menus": false, "seo_title": "", "search_description": "", "first_published_at": "2018-09-29T09:28:12.201736Z", "parent": { "id": 3, "meta": { "type": "home.HomePage", "detail_url": "http://localhost/api/v2/pages/3/", "html_url": "http://localhost/" }, "title": "Home" } }, ...
... "title": "Stream page title", "date": "2018-09-29", "body": [ { "type": "heading", "value": "This is a heading", "id": "7def6f56-f25b-4237-891b-b13bd1acc070" }, { "type": "paragraph", "value": "<p>First paragraph <b>Bold</b></p>", "id": "aaeb024c-3f88-48ca-b766-63e9319cf097" }, { "type": "image", "value": 4, "id": "9b31099e-334c-455a-98ab-1a124a38cfc3" }, { "type": "paragraph", "value": "<p>Second paragraph after an image</p>", "id": "72709eff-5521-4496-9b02-99fe2c5ee349" } ] }
$ pip install "elasticsearch>=6.0.0,<6.3.1"
# settings.py WAGTAILSEARCH_BACKENDS = { 'default': { 'BACKEND': 'wagtail.search.backends.elasticsearch2', 'URLS': ['http://localhost:9200'], 'INDEX': 'wagtail', 'TIMEOUT': 5, 'OPTIONS': {}, 'INDEX_SETTINGS': {}, } }
# settings.py INSTALLED_APPS = [ ... "wagtail.contrib.frontend_cache" ] WAGTAILFRONTENDCACHE = { 'varnish': { 'BACKEND': 'wagtail.contrib.frontend_cache.backends.HTTPBackend', 'LOCATION': 'http://localhost:8000', }, } WAGTAILFRONTENDCACHE_LANGUAGES = []