A rout-able page is basically a page that wagtail doesn’t have too much control over
INSTALLED_APPS = [
...
"wagtail.contrib.routable_page"
inside home’s models.py:
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from django.shortcuts import render
class HomePage(RoutablePageMixin, Page):
"""
Home Page Model
"""
# Define templates that can be used with this model
templates = "home/home_page.html"
...
...
@route(r'^$') # empty regex
def the_subscribe_page(self, request, *args, **kwargs):
context = self.get_context(request, *args, **kwargs)
context["a_special_test"] = "Hello World!"
return render(request, "home/subscribe.html", context)
Create a subsribers template, the routable page will now over ride it!
{% extends 'base.html' %}
{%block content%}
This is a test page {{ a_special_test }}
{% endblock %}
We can now change the regex to route the user to where we want them to go :)
@route(r'^subscribe/$')
def the_subscribe_page(self, request, *args, **kwargs):
context = self.get_context(request, *args, **kwargs)
context["a_special_test"] = "Hello World!"
return render(request, "home/subscribe.html", context)
We can go to the blog app and change the blog listing page to add a routable page that shows only the latest blog!
def get_context(self, request, *args, **kwargs):
"""Adding Custom stuff to our context."""
context = super().get_context(request, *args, **kwargs)
context["posts"] = BlogDetailPage.objects.live().public()
return context
@route(r'^latest/$')
def latest_blog_posts(self, request, *args, **kwargs):
context = self.get_context(request, *args, **kwargs)
context["latest_post"] = BlogDetailPage.objects.live().public()[:1]
return render(request, 'blog/latest_posts.html', context)
With a routable URL, you can add additional context rather than putting it all in the original context!!