| 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, HttpResponseRedirect |
|---|
| 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.endswith('/') and settings.APPEND_SLASH: |
|---|
| 23 |
return HttpResponseRedirect("%s/" % request.path) |
|---|
| 24 |
if not url.startswith('/'): |
|---|
| 25 |
url = "/" + url |
|---|
| 26 |
f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID) |
|---|
| 27 |
# If registration is required for accessing this page, and the user isn't |
|---|
| 28 |
# logged in, redirect to the login page. |
|---|
| 29 |
if f.registration_required and not request.user.is_authenticated(): |
|---|
| 30 |
from django.contrib.auth.views import redirect_to_login |
|---|
| 31 |
return redirect_to_login(request.path) |
|---|
| 32 |
if f.template_name: |
|---|
| 33 |
t = loader.select_template((f.template_name, DEFAULT_TEMPLATE)) |
|---|
| 34 |
else: |
|---|
| 35 |
t = loader.get_template(DEFAULT_TEMPLATE) |
|---|
| 36 |
|
|---|
| 37 |
# To avoid having to always use the "|safe" filter in flatpage templates, |
|---|
| 38 |
# mark the title and content as already safe (since they are raw HTML |
|---|
| 39 |
# content in the first place). |
|---|
| 40 |
f.title = mark_safe(f.title) |
|---|
| 41 |
f.content = mark_safe(f.content) |
|---|
| 42 |
|
|---|
| 43 |
c = RequestContext(request, { |
|---|
| 44 |
'flatpage': f, |
|---|
| 45 |
}) |
|---|
| 46 |
response = HttpResponse(t.render(c)) |
|---|
| 47 |
populate_xheaders(request, response, FlatPage, f.id) |
|---|
| 48 |
return response |
|---|