一个模板 - 多个模型错误

发布于 2024-10-21 22:04:20 字数 818 浏览 6 评论 0原文

views.py

def fadded(request):
    if request.method == "POST":
        fform = FtForm(request.POST)
        bform = BgForm(request.POST)
        if fform.is_valid() and bform.is_valid():
            bcontent=bform.save()
            fcontent=fform.save()

        else:
            return render_to_response("ft.html", {
            "fform": fform,
            "bform": bform,
            },context_instance=RequestContext(request))
    return HttpResponse('OK!')

ft.html

...
    {% if form.errors%}
    
    {% for error in form.errors %} {{ error|escape }} {% endfor %}
{% endif %} ...

有两种模型形式:fform 和 bform。它们代表两个不同的模型,但在同一模板中使用。我正在尝试保存两者并从两者中获取表单/字段错误。但如果已经存在 fform.errors,django 不会显示 bform.errors(并且可能甚至不会创建 bform)。有什么不同的建议吗?

views.py

def fadded(request):
    if request.method == "POST":
        fform = FtForm(request.POST)
        bform = BgForm(request.POST)
        if fform.is_valid() and bform.is_valid():
            bcontent=bform.save()
            fcontent=fform.save()

        else:
            return render_to_response("ft.html", {
            "fform": fform,
            "bform": bform,
            },context_instance=RequestContext(request))
    return HttpResponse('OK!')

ft.html

...
    {% if form.errors%}
    
    {% for error in form.errors %} {{ error|escape }} {% endfor %}
{% endif %} ...

There are two modelforms: fform and bform. They represent two different models, but are used in same template. I'm trying to save both and to get form-/fielderrors from both. But if there are already fform.errors, django doesn't shows bform.errors(and propably doesn't even create bform). Any proposals for a different way?

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

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

发布评论

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

评论(2

多像笑话 2024-10-28 22:04:20

django 不显示 bform.errors(并且
可能甚至没有创建 bform)

根据您的设置,两个表单都会传递数据并准备好进行验证。应该没有问题。

在您的模板中,您必须显示两种表单错误(我只看到模板中检查了一种表单)

{{ fform.errors }} <!-- show errors from fform -->
{{ bform.errors }} <!-- show errors from bform -->

django doesn't shows bform.errors(and
propably doesn't even create bform)

Given your setup, both forms are passed data and are ready to be validated. There shouldn't be a problem.

In your template, you'd have to display both forms errors (I only see one form being checked in your template)

{{ fform.errors }} <!-- show errors from fform -->
{{ bform.errors }} <!-- show errors from bform -->
ㄟ。诗瑗 2024-10-28 22:04:20

您需要使用基于类的视图!

以下是在一个 Django 视图中使用多个表单的快速示例。

从 django.contrib 导入消息
从 django.views.generic 导入 TemplateView

从 .forms 导入 AddPostForm、AddCommentForm
from .models import Comment

class AddCommentView(TemplateView):

    post_form_class = AddPostForm
    comment_form_class = AddCommentForm
    template_name = 'blog/post.html'

    def post(self, request):
        post_data = request.POST or None
        post_form = self.post_form_class(post_data, prefix='post')
        comment_form = self.comment_form_class(post_data, prefix='comment')

        context = self.get_context_data(post_form=post_form,
                                        comment_form=comment_form)

        if post_form.is_valid():
            self.form_save(post_form)
        if comment_form.is_valid():
            self.form_save(comment_form)

        return self.render_to_response(context)     

    def form_save(self, form):
        obj = form.save()
        messages.success(self.request, "{} saved successfully".format(obj))
        return obj

    def get(self, request, *args, **kwargs):
        return self.post(request, *args, **kwargs)

在这个例子中,我在网上找到了(在 RIP教程),我们使用模板视图。基于类的视图是解决这个问题的方法。以下是有关如何在 Django 上使用基于类的视图的最新文档的链接。享受阅读的乐趣,最重要的是要有耐心。 https://docs.djangoproject.com/en/2.2/topics /class-based-views/

希望这可以帮助引导您走向正确的方向。期待听到事情进展如何。

You need to use Class based views!

Here is a quick example of using multiple forms in one Django view.

from django.contrib import messages
from django.views.generic import TemplateView

from .forms import AddPostForm, AddCommentForm
from .models import Comment

class AddCommentView(TemplateView):

    post_form_class = AddPostForm
    comment_form_class = AddCommentForm
    template_name = 'blog/post.html'

    def post(self, request):
        post_data = request.POST or None
        post_form = self.post_form_class(post_data, prefix='post')
        comment_form = self.comment_form_class(post_data, prefix='comment')

        context = self.get_context_data(post_form=post_form,
                                        comment_form=comment_form)

        if post_form.is_valid():
            self.form_save(post_form)
        if comment_form.is_valid():
            self.form_save(comment_form)

        return self.render_to_response(context)     

    def form_save(self, form):
        obj = form.save()
        messages.success(self.request, "{} saved successfully".format(obj))
        return obj

    def get(self, request, *args, **kwargs):
        return self.post(request, *args, **kwargs)

In this example I have found online (on RIP tutorial), we use TEMPLATE VIEW. Class-based views are your way around this. Here is a link to the most up-to-date documentation on how to use class-based views on Django. Have fun reading, and most of all, patience. https://docs.djangoproject.com/en/2.2/topics/class-based-views/

Hopefully, this could help to guide you in the right direction. Looking forward to hearing from how it goes.

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