Skip to content

Instantly share code, notes, and snippets.

@schwuk
Last active September 23, 2017 20:44
Show Gist options
  • Save schwuk/5037899 to your computer and use it in GitHub Desktop.
Save schwuk/5037899 to your computer and use it in GitHub Desktop.
Django class-based view(CBV) mixin to include flatpage content in the context.
from django.contrib.flatpages.models import FlatPage
class FlatPageMixin(object):
"""
Retrieves the FlatPage object for the specified url, and includes it in the
context.
If no url is specified, request.path is used.
"""
url = None
def get_context_data(self, **kwargs):
if not self.url:
self.url = self.request.path
context = super(FlatPageMixin, self).get_context_data(**kwargsG)
try:
flatpage = FlatPage.objects.get(url=self.url)
flatpage.title = mark_safe(flatpage.title)
flatpage.content = mark_safe(flatpage.content)
context["flatpage"] = flatpage
except FlatPage.DoesNotExist:
context["flatpage"] = None
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment