3

我有多个可以转换为平面页面的模型,但必须有一些额外的信息(例如,我有一个关于我们的页面,但我也有一个博客)。
但是我知道必须只有一个平面模型,因为中间件只返回平面实例并且不解析子模型。
我需要做什么?

编辑:
看来我需要改变观点。
这是当前代码:

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, HttpResponseRedirect
from django.conf import settings
from django.core.xheaders import populate_xheaders
from django.utils.safestring import mark_safe
from django.views.decorators.csrf import csrf_protect

DEFAULT_TEMPLATE = 'flatpages/default.html'

# This view is called from FlatpageFallbackMiddleware.process_response
# when a 404 is raised, which often means CsrfViewMiddleware.process_view
# has not been called even if CsrfViewMiddleware is installed. So we need
# to use @csrf_protect, in case the template needs {% csrf_token %}.
# However, we can't just wrap this view; if no matching flatpage exists,
# or a redirect is required for authentication, the 404 needs to be returned
# without any CSRF checks. Therefore, we only
# CSRF protect the internal implementation.
def flatpage(request, url):
    """
    Public interface to the flat page view.

    Models: `flatpages.flatpages`
    Templates: Uses the template defined by the ``template_name`` field,
        or `flatpages/default.html` if template_name is not defined.
    Context:
        flatpage
            `flatpages.flatpages` object
    """
    if not url.endswith('/') and settings.APPEND_SLASH:
        return HttpResponseRedirect("%s/" % request.path)
    if not url.startswith('/'):
        url = "/" + url

    # Here instead of getting the flat page it needs to find if it has a page with a child model.
    f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID)
    return render_flatpage(request, f)

@csrf_protect
def render_flatpage(request, f):
    """
    Internal interface to the flat page view.
    """
    # If registration is required for accessing this page, and the user isn't
    # logged in, redirect to the login page.
    if f.registration_required and not request.user.is_authenticated():
        from django.contrib.auth.views import redirect_to_login
        return redirect_to_login(request.path)
    if f.template_name:
        t = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
    else:
        t = loader.get_template(DEFAULT_TEMPLATE)

    # To avoid having to always use the "|safe" filter in flatpage templates,
    # mark the title and content as already safe (since they are raw HTML
    # content in the first place).
    f.title = mark_safe(f.title)
    f.content = mark_safe(f.content)

    # Here I need to be able to configure what I am passing in the context
    c = RequestContext(request, {
        'flatpage': f,
    })
    response = HttpResponse(t.render(c))
    populate_xheaders(request, response, FlatPage, f.id)
    return response
4

1 回答 1

1

你这是错的。根本不需要改变观点。contrib Flatpages 应用程序是为一个特定目的而构建的,“平面页面”即类似于“关于”页面的页面,因此请使用它。对于博客,您将(现在或将来)使用截然不同的功能,因此请为此使用单独的应用程序。已经有几个“博客”应用程序,或者编写您自己的简单应用程序。

从您的问题来看,您似乎已经编写了一些从 Flatpages 继承的模型,我猜这没问题,但对于我来说,这么小的模型开始时似乎有点傻。如果是这种情况,您需要做的是在您自己的应用程序中编写自己的视图,使用这些模型,而不是期望 Flatpage 应用程序知道任何关于它们的信息。希望有帮助!

于 2010-12-29T19:12:30.847 回答