Login

Class-based view mixin for flatpages

Author:
schwuk
Posted:
February 26, 2013
Language:
Python
Version:
1.3
Tags:
mixin flatpages class-based-views
Score:
0 (after 0 ratings)

Allows you to include content from flatpages in class-based views. You can specify the url for the flatpage you want, or let it be determined by request.path.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

More like this

  1. Django BasicAuthMixin by icrni 9 months, 3 weeks ago
  2. Basic PDF view mixin and utils using reportlab. by elequ 1 year, 8 months ago
  3. View mixin and utils to generate PDF documents from html using xhtml2pdf by frankban 3 years, 6 months ago
  4. LoginRequiredMixin by slink 3 years, 10 months ago
  5. Easier and Faster than flatpages. Rendering templates by simpling calling by his path on the URL by mariocesar 3 years ago

Comments

Please login first before commenting.