Ho un modello di autore e un modello di libri. Un utente può modificare le proprietà di tutti i libri di un determinato autore. Voglio essere in grado di visualizzare gli errori per ogni singolo libro piuttosto che avere tutti gli errori elencati in alto, Come posso fare questo?Django: come visualizzare gli errori di modulo per ogni oggetto del modello in un set di forme in linea
MODELLI
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
VISTA
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', {'author': a, 'authorsbooks': authorsbooks, 'formset': formset,}, context_instance=RequestContext(request))
MODELLO
#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>
modello aggiornato: non visualizza errori
<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"/>
Puoi aggiungere la fonte per il modello Libri e Autore in modo che possa provarlo? grazie;) – jpic
@jpic ha aggiunto i modelli per ya – thedeepfield
"get_object_or_404 (author, pk = author_id)" dovrebbe essere "get_object_or_404 (Autore, pk = author_id)" (Autore in maiuscolo) giusto? Inoltre, sei sicuro che il tuo modello si chiami Book? Sembra che dovrebbe essere libri? – jpic