Root middleware for pages (
Page middleware (
The guide recommends using a middleware for the feincms3 pages app. This module
offers helpers and utilities to reduce the amount of code you have to write.
The reason why this module is called
root is that the page app’s mountpoint
has to be the Python app’s mountpoint when using this. If that’s not the case
you may want to write your own URLs and views.
Example code for using this module (e.g.
from django.shortcuts import render from feincms3.root.middleware import add_redirect_handler, create_page_if_404_middleware from app.pages.models import Page from app.pages.utils import page_context # The page handler receives the request and the page. # ``add_redirect_handler`` wraps the handler function with support for the # RedirectMixin. @add_redirect_handler def handler(request, page): return render(request, page.type.template_name, page_context(request, page=page)) # This is the middleware which you want to add to ``MIDDLEWARE`` as # ``app.pages.middleware.page_if_404_middleware``. The middleware should be # added in the last position except if you have a very good reason not to # do this. page_if_404_middleware = create_page_if_404_middleware( # queryset=Page.objects.active() works too (if .active() doesn't use # get_language or anything similar) queryset=lambda request: Page.objects.active(), handler=handler, )
Wrap the page handler in a redirect mixin handler
- feincms3.root.middleware.create_page_if_404_middleware(*, queryset, handler, language_code_redirect=False)[source]#
Create a middleware for handling pages
This utility is there for your convenience, you do not have to use it. The returned middleware already handles returning non-404 responses as-is, fetching a page instance from the database and calling a user-defined handler on success. It optionally also supports redirecting requests to the root of the app to a language-specific landing page.
queryset: A page queryset or a callable accepting the request and returning a page queryset.
handler: A callable accepting the request and a page and returning a response.
False): Redirect visitor to the language code prefix (e.g.
/de-ch/) if request path equals the script prefix (generally
/) and no active page for
Passthru page apps (
Create an application type:
TYPES = [ ... ApplicationType( key="imprint", title=_("imprint"), urlconf="feincms3.root.passthru", template_name="pages/standard.html", regions=[Region(key="main", title=_("Main"))], ), ]
Reverse the URL of the page (if it exists):
# Raise NoReverseMatch on failure reverse_passthru("imprint") # Fallback reverse_passthru("imprint", fallback="/en/imprint/") # Outside the request-response cycle reverse_passthru("imprint", urlconf=apps_urlconf())