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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:39:07+00:00 2026-06-14T20:39:07+00:00

I’m really stuck with this. To show my problem I created a new Django

  • 0

I’m really stuck with this. To show my problem I created a new Django project and started from scratch, focusing only on one single form.

What I’m trying to do is to create a form with several fields of the same name. I tried using modelformset_factory to achieve this but it looks to me like it’s not what I really need.

Below is my code (also on dpaste) which currently works fine with one single field called name. How can I create and process a form which would have several name fields? Could somebody point me in the right direction?

# 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',)

# views.py
def home(request):
    if request.method == 'POST':
        catform = CategoryForm(request.POST)
        catformInstance = catform.save(commit = False)
        catformInstance.save()
        return HttpResponseRedirect('')
    else:
        catform = CategoryForm() 

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

# home.html template
<h3>Insert new Category</h3> 
<form action="/" method="post" id="ingr-cat-form">{% csrf_token %}
{{ catform.as_p }}
<input type="submit" name="ingrCatForm" value="Save" />
</form>

UPDATE: to clarify, I want to allow user to insert several categories within one form. I think I’m getting close, here is my new version of views.py but it still stores just one category (the last one in the list):

def home(request):
    if request.method == 'POST':
        catform = CategoryForm(request.POST)
        names = request.POST.getlist('name')
        catformInstance = catform.save(commit = False)
        for name in names:
            catformInstance.name = name
            catformInstance.save()
        return HttpResponseRedirect('')
    else:
        catform = CategoryForm() 

    context = {'catform': catform}
    return render_to_response('home.html', context, context_instance=RequestContext(request))
  • 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-14T20:39:08+00:00Added an answer on June 14, 2026 at 8:39 pm

    You cannot have fields with the same name (on the same Model). If you only need to change the html label in the html form, use

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

    or

    class CategoryForm(ModelForm):
        def __init__(self , *args, **kwargs):
            super(CategoryForm, self).__init__(*args, **kwargs)
            self.fields['name2'].label = "name"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.