Django:如何在内联formset中显示每个模型对象的表单错误

前端之家收集整理的这篇文章主要介绍了Django:如何在内联formset中显示每个模型对象的表单错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个作者模型和书籍模型.用户可以修改给定作者的所有书籍的属性.我希望能够为每本书显示错误,而不是列出顶部的所有错误,我该怎么做?

楷模

from django.db import models
from django.forms import ModelForm,Textarea
from django import forms

class Author(models.Model):
    fname = models.CharField(max_length=100)
    lname = models.CharField(max_length=100)
    def fullname(self):
        return '%s %s' % (self.fname,self.lname)
    fullname = property(fullname)
    def __unicode__(self):
        return self.fullname

class Books(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=50)
    publisher = models.CharField(max_length=50)
    edition = models.CharField(max_length=50)
    comment = models.TextField()
    def __unicode__(self):
         return self.title

视图

def author_books_edit(request,author_id):
    a = get_object_or_404(Author,pk=author_id)
    authorsbooks = a.books_set.all()
    bookformset = inlineformset_factory(Author,Books,can_delete=True,can_order=True,exclude=('company',),extra=1)
    formset = bookformset(instance=a)
    if request.method == "POST":
        formset = bookformset(request.POST,request.FILES,instance=a)
        if formset.is_valid():
            formset.save()
        else:
            form_errors = formset.errors
            return render_to_response('test/authors_books_edits.html',{'author': a,'authorsbooks': authorsbooks,'formset': formset,'form_errors': form_errors},context_instance=RequestContext(request))
    return render_to_response('test/authors_books_edits.html',},context_instance=RequestContext(request))

模板

#all errors are here
{% for dict in form_errors %}
    {{ dict }}
{% endfor %}

#all forms are here,i want to pair the errors for each form
<form method="post" action="/test/{{ author.id }}/books/">
    {% csrf_token %}
        <table>
        {{ formset }}
        </table>
    <input type="submit" value="Submit"/>
    </form>

UPDATED TEMPLATE:不显示错误

<form method="post" action="/test/{{ author.id }}/books/">
    {% formset.management_form %}
    {% csrf_token %}
    <table>
        {% for x in formset %}
        {{x.errors }}
        {{ x }}
        {% endfor %}
    </table>
<input type="submit" value="Submit"/>

解决方法

编辑

authors_books_edits.html

<form method="post" action="/test/{{ author.id }}/books/">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset.forms %}
        {{ form.non_field_errors }}
        {{ form.errors }}
        <table>
            {{ form.as_table }}
        </table>
    {% endfor %}
    <input type="submit" value="Submit"/>
</form>

views.py

from django.shortcuts import *
from django.forms.models import inlineformset_factory

from .models import *

def author_books_edit(request,instance=a)
        if formset.is_valid():
            formset.save()
        else:
            form_errors = formset.errors
            return render_to_response('authors_books_edits.html',context_instance=RequestContext(request))
    return render_to_response('authors_books_edits.html',context_instance=RequestContext(request))

models.py

from django.db import models

class Author(models.Model):
    fname = models.CharField(max_length=100)
    lname = models.CharField(max_length=100)
    def fullname(self):
        return '%s %s' % (self.fname,self.lname)
    fullname = property(fullname)
    def __unicode__(self):
        return self.fullname

class Books(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=50)
    publisher = models.CharField(max_length=50)
    edition = models.CharField(max_length=50)
    comment = models.TextField()
    def __unicode__(self):
         return self.title

urls.py

from django.conf.urls.defaults import patterns,include,url

urlpatterns = patterns('testapp.views',url(r'test/(?P<author_id>\d+)/books/$','author_books_edit'),)

您可以制作另一个临时应用来测试它.

它看起来像这样:http://imageshack.us/photo/my-images/824/screenshotat20120227190.png/

==结束编辑

您可以这样迭代表单:

{% for form in formset.forms %}
   {{ form }}
{% endfor %}

在这种情况下,请参阅Django使用模板文档显示表单:https://docs.djangoproject.com/en/dev/topics/forms/#displaying-a-form-using-a-template

然后,更有趣的是,自定义表单模板(请参阅form.non_field_errors):
https://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

原文链接:https://www.f2er.com/python/186347.html

猜你在找的Python相关文章