将表单添加到 django-flatpages

发布于 2024-12-22 13:48:47 字数 940 浏览 3 评论 0原文

我有一个联系人&使用平面页面的信息页面。不过,我想添加一个表单,允许访问者将他们的电子邮件签名到列表中。但是,如果我发送 URL 来呈现表单视图,它将不具有平面页面上下文。在我的电子邮件表单视图中包含平面页面上下文,或将我的表单上下文添加到我的平面页面中的最佳方法是什么。


感谢 Tommasa Barbugli 提供的解决方案。我现在有:

from django.contrib.flatpages.models import FlatPage
from django.shortcuts import render, get_object_or_404

from canada.bulkmail.models import *

def email(request):
    contact_flatpage = get_object_or_404(FlatPage, url='/contact/')
    ContactEmailFormSet = modelformset_factory(Contact, formset=ContactEmailForm)
    if request.method == 'POST':
        formset = ContactEmailFormSet(request.POST)
        if formset.is_valid():
            contact = formset.save()
    else:
        formset = ContactEmailFormSet()
    return render(request, 'flatpages/default.html', {
        "formset": formset,
        "flatepage": contact_flatpage,
    })

I have a a contact & info page that uses flatpages. However I want to add a form that allows visitors to sign their emails up to a list. However, if I send the URL to render the form view, it won't have the flatpages context. What is the best way to either include the flatpages context on my email form view, or add my form context to my flatpage.


Thanks to Tommasa Barbugli for the solution. I now have:

from django.contrib.flatpages.models import FlatPage
from django.shortcuts import render, get_object_or_404

from canada.bulkmail.models import *

def email(request):
    contact_flatpage = get_object_or_404(FlatPage, url='/contact/')
    ContactEmailFormSet = modelformset_factory(Contact, formset=ContactEmailForm)
    if request.method == 'POST':
        formset = ContactEmailFormSet(request.POST)
        if formset.is_valid():
            contact = formset.save()
    else:
        formset = ContactEmailFormSet()
    return render(request, 'flatpages/default.html', {
        "formset": formset,
        "flatepage": contact_flatpage,
    })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-12-29 13:48:47

从表单页面视图返回正确的上下文。

这是发送到 Flatpages 模板的上下文(其中 f 是 Fl​​atpage 实例),

f.title = mark_safe(f.title)
f.content = mark_safe(f.content)

c = RequestContext(request, {
    'flatpage': f,
})

在您的视图中,您可以轻松返回包含您需要的所有属性的(未保存的)Flatpage 实例。

f = FlatPage()
f.title = ""
f.content = ""
....
c = RequestContext(request, {
    'flatpage': f,
})

Return the right context from the form page view.

This is the context sent to flatpages' template (where f is the flatpage instance)

f.title = mark_safe(f.title)
f.content = mark_safe(f.content)

c = RequestContext(request, {
    'flatpage': f,
})

in your view you can easily return a (unsaved) flatpage instance with all attrs you need.

f = FlatPage()
f.title = ""
f.content = ""
....
c = RequestContext(request, {
    'flatpage': f,
})
习惯成性 2024-12-29 13:48:47

如果您要使用表单,那么您不需要使用平面页面 - 使用带有平面页面模板的常规视图,并使用 RequestContext 作为上下文。

If you are going to be using forms, then you don't need to use flatpage - use a regular view with the flatpage template, and RequestContext as your context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文