2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.flatpages.models import FlatPage
|
|
|
|
from django.template import loader, RequestContext
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.conf import settings
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
DEFAULT_TEMPLATE = 'flatpages/default.html'
|
2005-09-24 06:09:42 +08:00
|
|
|
|
2005-11-11 12:45:05 +08:00
|
|
|
def flatpage(request, url):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2005-11-11 12:45:05 +08:00
|
|
|
Flat page view.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-11 12:45:05 +08:00
|
|
|
Models: `flatpages.flatpages`
|
2005-07-13 09:25:57 +08:00
|
|
|
Templates: Uses the template defined by the ``template_name`` field,
|
2006-05-02 09:31:56 +08:00
|
|
|
or `flatpages/default.html` if template_name is not defined.
|
2005-07-13 09:25:57 +08:00
|
|
|
Context:
|
2005-11-11 12:45:05 +08:00
|
|
|
flatpage
|
|
|
|
`flatpages.flatpages` object
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
if not url.startswith('/'):
|
|
|
|
url = "/" + url
|
2006-05-02 09:31:56 +08:00
|
|
|
f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID)
|
2005-07-13 09:25:57 +08:00
|
|
|
# If registration is required for accessing this page, and the user isn't
|
|
|
|
# logged in, redirect to the login page.
|
2005-11-11 12:45:05 +08:00
|
|
|
if f.registration_required and request.user.is_anonymous():
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.auth.views import redirect_to_login
|
2005-07-13 09:25:57 +08:00
|
|
|
return redirect_to_login(request.path)
|
2005-09-24 06:09:42 +08:00
|
|
|
if f.template_name:
|
2006-05-02 09:31:56 +08:00
|
|
|
t = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
|
2005-09-24 06:09:42 +08:00
|
|
|
else:
|
2006-05-02 09:31:56 +08:00
|
|
|
t = loader.get_template(DEFAULT_TEMPLATE)
|
|
|
|
c = RequestContext(request, {
|
2005-11-11 12:45:05 +08:00
|
|
|
'flatpage': f,
|
2005-07-13 09:25:57 +08:00
|
|
|
})
|
|
|
|
return HttpResponse(t.render(c))
|