Last active
September 23, 2017 20:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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