Skip to content

Instantly share code, notes, and snippets.

@andrewschoen
Created May 14, 2012 14:10
Show Gist options
  • Save andrewschoen/2694206 to your computer and use it in GitHub Desktop.
Save andrewschoen/2694206 to your computer and use it in GitHub Desktop.
Django middleware to create a 'page header' in django-cms navigation.
from django import http
class NotAPageMiddleware(object):
"""
This middleware checks to see if the cms page is assigned to a specific template
and if so will redirect to the next published child page. Needs to be after the
cms page middleware.
"""
def process_response(self, request, response):
in_admin = request.path.split('/')[1] == "admin"
if hasattr(request, "current_page") and not in_admin:
page = request.current_page
if page:
if page.template == "not-a-page.html":
if page.children.filter(published=True).count() > 0:
child = page.children.filter(published=True)[0]
return http.HttpResponseRedirect(child.get_absolute_url())
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment