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 6541537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:07:00+00:00 2026-05-25T11:07:00+00:00

I have been following this useful-looking article http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/ . Unfortunately he doesn’t hint at

  • 0

I have been following this useful-looking article http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/. Unfortunately he doesn’t hint at the template code, so I’m guessing there.

I want to create a bunch of forms in a page, to add multiple objects to a model, with some common properties set by another form either in the page’s variables or at the top of the page. This is for a teacher’s marksheet.

My views.py:

def record_assessments(request, teachinggroup, objective):
    theclass = TeachingGroup.objects.get(name__iexact = teachinggroup)
    pupils = Pupil.objects.filter(teaching_group = theclass)
    theobjective = Objective.objects.get(code = objective)
    thedate = datetime.date.today()

    if request.method == 'POST':
        aforms = [PupilAssessmentForm(request.POST, prefix=x.id, instance=Assessment()) for x in pupils]
        if all(af.is_valid() for af in aforms):
            for af in aforms:
                new_record = af.save(commit = False)
                new_record.objective = theobjective
                new_record.date = thedate
                new_record.save()
            return redirect("/app/" + theclass + "/" + marksheet + "/" + theobjective.strand.code|lower + "/")
    else:
        aforms = [PupilAssessmentForm(prefix=str(x.id)) for x in pupils]
    return render_to_response('recordassessments.html', locals())

I haven’t managed to check the logic in the first if loop yet, as I haven’t managed to POST the form properly yet.

The page renders properly if I put

    else:
        aforms = [PupilAssessmentForm(prefix=str(x.id), instance=x) for x in pupils]

But then I’m tying a ModelForm from the Assessment model to an object in the Pupil model, which seems wrong.

My template:

{% for af in aforms %}
<form action="" method="post">
{{af.instance}}{{ af.errors }}
<p>
{{ af }}
{% endfor %}
<input type="submit" value="Submit">
</form>

The error (selected snippets):

Exception Type: TemplateSyntaxError
Exception Value:    Caught DoesNotExist while rendering:

error at line 20
Caught DoesNotExist while rendering: 
20    {% for af in aforms %}

And yet the aforms list appears in the page variables:

aforms  
[<two.app.forms.PupilAssessmentForm object at 0x21db0d0>,
 <two.app.forms.PupilAssessmentForm object at 0x21db650>]
  • 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-25T11:07:01+00:00Added an answer on May 25, 2026 at 11:07 am

    I’m not sure why, but I managed to do it by changing the list to a dictionary, which suited my needs anyway for displaying the form as a table row per pupil together with some other assessment data. My original question remains open for anyone who knows an alternative method that more directly addresses the question I was originally asking, but here’s how I did it:

    views.py (the relevant bits):

        if request.method == 'POST':
            assessment_grid = {}
            for x in pupils:
                form = PupilAssessmentForm(request.POST, prefix=str(x.id))
                try:
                    assessment = Assessment.objects.filter(objective = theobjective).filter(pupil = x).filter(date__lte = thedate).latest('date')
                except Assessment.DoesNotExist:
                    assessment = None
                pupil_row = [assessment, form]
                assessment_grid[x] = pupil_row
            if all(pupil_row[1].is_valid() for pupil, pupil_row in assessment_grid.items()):
                for pupil, pupil_row in assessment_grid.items():
                    new_record = pupil_row[1].save(commit = False)
                    new_record.objective = theobjective
                    new_record.date = thedate
                    new_record.teacher = theclass.teacher
                    new_record.pupil = pupil
                    new_record.save()
                return redirect("some link")
         else:
            assessment_grid = {}
            for x in pupils:
                form = PupilAssessmentForm(prefix=str(x.id))
                try:
                    assessment = Assessment.objects.filter(objective = theobjective).filter(pupil = x).filter(date__lte = thedate).latest('date')
                except Assessment.DoesNotExist:
                    assessment = None
                pupil_row = [assessment, form]
                assessment_grid[x] = pupil_row
        return render_to_response('recordassessments.html', locals())
    

    I’m not sure whether there’s a more elegant way to assemble the dictionary, as mine isn’t particularly DRY. And I have no idea why I could unpack a dictionary containing a form yet not a list. Was my syntax wrong? Anyway: problem sorted; page renders.

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

Sidebar

Related Questions

I'm new to NHibernate... I have been following this NHibernate Tutorial from Gabriel Schenker
I have been working on the following problem from this book . A certain
I can't believe how unbelievably complicated this has been... I have the following XML...
I have been following the following post on using multiple ItemTemplates in a ListView
I have been following the guidance given on this other post: Assigning an IronPython
I have been following the usage of JavaScript for the past few years, and
I have been following mozilla developer network's docs and created a transition for my
I have been following a couple of articles regarding RESTful web services with WCF
I have been following the validation for Entry boxes from here . The code
I have been following the affableBean tutorial from the NetBeans site located here .

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.