Documentation examples for overriding Page.serve should not bypass get_context
See original GitHub issueI had a couple of Pages that overrode the serve() method and noticed that the userbar wouldn’t display on these pages.
Digging through the source I figured out you have to add wagtailcore.models.PAGE_TEMPLATE_VAR to your context with a reference to the page instance.
For example:
from django.shortcuts import render
from wagtail.wagtailcore.models import Page, PAGE_TEMPLATE_VAR
class CustomPage(Page):
def serve(self, request, *args, **kwargs):
return render(request, self.template, {
PAGE_TEMPLATE_VAR: self,
'self': self
})
I’m not sure if this is a documentation issue or a consistency issue with which context variable will be used as a reference to the Page instance. In the tutorial and docs it shows using ‘self’ but it looks like PAGE_TEMPLATE_VAR is using ‘page’.
Thanks!
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Context | Android Developers
MODE_MULTI_PROCESS does not work reliably in some versions of Android, ... For instance, a View associated with a restricted context would ignore particular ......
Read more >Using mixins with class-based views - Django documentation
A common reason to override get_queryset() here would be to dynamically vary ... Not all mixins can be used together, and not all...
Read more >Flutter get context in initState method - dart - Stack Overflow
What I'm trying to achieve is to check when the page is rendered to perform some checks and based on them opening a...
Read more >Page models — Wagtail Documentation 4.1.1 documentation
get_url_parts(request) method will not typically be called directly, but may be overridden to define custom URL routing for a given page model. It...
Read more >How To Use Contexts in Go - DigitalOcean
For example, if a web server function is handling an HTTP request for a ... You can use this as a placeholder when...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is a result of #1571 - as of Wagtail 1.1, we set the ‘self’ and ‘page’ variables side by side (and use ‘page’ in the userbar logic) to accommodate the Jinja2 template engine, which doesn’t like us using the name ‘self’. We haven’t covered this in the documentation yet because full Jinja2 support is still in development.
To guard against changes like this, it’s probably best to use
get_context
rather than building your own context dictionary:and override get_context (using
super
) if you want to add extra variables there. We should update our documentation to demonstrate that pattern.It would also be worth updating our userbar code to gracefully deal with the missing
page
variable though: ifpage
is defined and refers to a page instance then use that, otherwise ifself
is defined and refers to a page instance then use that. @timheap, does that sound like it would work?I was very happy with this logic:
I think this is a better layout than the one used in bakerydemo because it uses the same get_context with and without a tag.