app/django/contrib/flatpages/views.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 from django.contrib.flatpages.models import FlatPage
       
     2 from django.template import loader, RequestContext
       
     3 from django.shortcuts import get_object_or_404
       
     4 from django.http import HttpResponse
       
     5 from django.conf import settings
       
     6 from django.core.xheaders import populate_xheaders
       
     7 from django.utils.safestring import mark_safe
       
     8 
       
     9 DEFAULT_TEMPLATE = 'flatpages/default.html'
       
    10 
       
    11 def flatpage(request, url):
       
    12     """
       
    13     Flat page view.
       
    14 
       
    15     Models: `flatpages.flatpages`
       
    16     Templates: Uses the template defined by the ``template_name`` field,
       
    17         or `flatpages/default.html` if template_name is not defined.
       
    18     Context:
       
    19         flatpage
       
    20             `flatpages.flatpages` object
       
    21     """
       
    22     if not url.startswith('/'):
       
    23         url = "/" + url
       
    24     f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID)
       
    25     # If registration is required for accessing this page, and the user isn't
       
    26     # logged in, redirect to the login page.
       
    27     if f.registration_required and not request.user.is_authenticated():
       
    28         from django.contrib.auth.views import redirect_to_login
       
    29         return redirect_to_login(request.path)
       
    30     if f.template_name:
       
    31         t = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
       
    32     else:
       
    33         t = loader.get_template(DEFAULT_TEMPLATE)
       
    34 
       
    35     # To avoid having to always use the "|safe" filter in flatpage templates,
       
    36     # mark the title and content as already safe (since they are raw HTML
       
    37     # content in the first place).
       
    38     f.title = mark_safe(f.title)
       
    39     f.content = mark_safe(f.content)
       
    40 
       
    41     c = RequestContext(request, {
       
    42         'flatpage': f,
       
    43     })
       
    44     response = HttpResponse(t.render(c))
       
    45     populate_xheaders(request, response, FlatPage, f.id)
       
    46     return response