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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:45:48+00:00 2026-06-15T02:45:48+00:00

I’m trying to loop through form fields and store them in the database. The

  • 0

I’m trying to loop through form fields and store them in the database. The problem is that it’s always only the last field that gets stored. The previous ones are “skipped”. Can I process the forms in Django in the following way?

models.py:

class Category(models.Model):
    name = models.CharField(max_length=30, unique=True)
    user = models.ForeignKey(User, blank=True, null=True)

    class Meta:
        verbose_name_plural = "Ingredience Categories"

    def __unicode__(self):
        return self.name

forms.py

class CategoryForm(ModelForm):
    class Meta:
        model = Category
        fields = ('name',)

home.html template (I’m buildin my form “manually” because I want to be able to dynamically add more input fields via jQuery):

<h3>Insert New Categories</h3>
<form action="/" method="post" id="ingr-cat-form">{% csrf_token %}
    <p><label for="id_0-name">Name:</label> <input type="text" maxlength="30" name="name" id="id_0-name"></p>
    <p><label for="id_1-name">Name:</label> <input type="text" maxlength="30" name="name" id="id_1-name"></p>
    <p><label for="id_2-name">Name:</label> <input type="text" maxlength="30" name="name" id="id_2-name"></p>
    <input type="submit" name="ingrCatForm" value="Save" /> 
</form> 

views.py:

def home(request):
    if request.method == 'POST':
        catform = CategoryForm(request.POST, instance=Category()) # store bounded form to catform
        catformInstance = catform.save(commit = False)  # create instance for further modification, don't commit yet
        catformNames = request.POST.getlist('name') # get a list of input values whose element name is "name"
        for name in catformNames: # loop through all name elements
            catformInstance.name = name # modify form instance; insert current name
            catformInstance.save() # save the instance to the database
        return HttpResponseRedirect('')
    else:
        catform = CategoryForm(instance=Category())

    context = {'catform': catform}
    return render_to_response('home.html', context, context_instance=RequestContext(request))

Test Case Steps:

  1. Insert the following values in the 3 input fields: value1, value2, value3
  2. Press the submit button

Expected Result:

  • all 3 values are stored in the database

Actual Result:

  • only the last value (value3) is stored in the database
  • 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-06-15T02:45:49+00:00Added an answer on June 15, 2026 at 2:45 am

    You create only one instance of catformInstance, then alter it’s value each time in the loop. You are updating one row in the database instead of creating a new row each time.

    Do this instead:

        for name in request.POST.getlist('name'):
            catform = CategoryForm({'name': name}, instance=Category())
            catform.save()
    

    Note that we pass in each name explicitly instead of the whole set of POST values; in that case it’ll just grab the last name from the name list, then fail during the next iteration as name is no longer available from the request.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.