Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:51:16+00:00 2026-05-30T12:51:16+00:00

I have a author model and a books model. A user can modify properties

  • 0

I have a author model and a books model. A user can modify properties of all the books from a given author. I want to be able to display errors for each individual book rather than have all the errors listed on the top, How can I do this?

MODELS

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

VIEW

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))

TEMPLATE

#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: doesn’t display errors

<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"/>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:51:17+00:00Added an answer on May 30, 2026 at 12:51 pm

    EDIT

    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, 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('authors_books_edits.html', {'author': a, 'authorsbooks': authorsbooks, 'formset': formset, 'form_errors': form_errors}, context_instance=RequestContext(request))
        return render_to_response('authors_books_edits.html', {'author': a, 'authorsbooks': authorsbooks, 'formset': formset,}, 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'),
    )
    

    You can make another temporary app to test it.

    It looks like this: http://imageshack.us/photo/my-images/824/screenshotat20120227190.png/

    == END EDIT

    You can iterate over forms as such:

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

    In that case, refer to Django’s displaying a form using a template documentation: https://docs.djangoproject.com/en/dev/topics/forms/#displaying-a-form-using-a-template

    Then, more interesting, customizing a form template (see form.non_field_errors):
    https://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have models of books and people: from django.db import models class Book(models.Model): author
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
There is one model named Book, I want to get all books, but all
Let's say I have Book model and an Author model. I want to list
I have a model Author with a has_many relationship to model Book. Books are
I have two tables, Author and Book, where an author can have many books.
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have some information in my database like 'author', 'book' etc., that are all
I have a simple Book Author relationship class Author(models.Model): first_name = models.CharField(max_length=125) last_name =
This is my model: class User {...} class Book { User author; int number;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.