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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:33:39+00:00 2026-06-17T21:33:39+00:00

I use formset to produce extra fields, but i don’t know how to change

  • 0

I use formset to produce extra fields, but i don’t know how to change the label for extra field produced by formset.

My code:

class GetMachine(forms.Form):
    Number_of_Lines = forms.IntegerField(max_value=4)

class GetLine(forms.Form):
    beamline_name = forms.CharField(max_length=15, label='Name of Beamline-%i')

def install(request):
    MachineFormSet = formset_factory(GetMachine, extra=1)
    formset = MachineFormSet()
    if request.method == 'POST':
#        formset = MachineFormSet(request.POST) 
#        if formset.is_valid(): # All validation rules pass
        line_no = request.POST['form-0-Number_of_Lines']
        GetLineFormSet = formset_factory(GetLine, extra=int(line_no))
        formset = GetLineFormSet()
        return render_to_response('install.html', { 'formset': formset, 'action': 'step1'})
    return render_to_response('install.html', { 'formset': formset, })    

install.html template:

{% for form in formset.forms %}
{% for field in form %}
    <tr>
        <td>{{ field.label_tag }}</td>  <td>{{ field }}</td><td>{{ field.errors }}</td>
    </tr>
{% endfor %}
{% endfor %}

For example if the “Number_of_Lines” = 2, then i expect the next form with the labels,

Name of Beamline-1:
Name of Beamline-2:
  • 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-17T21:33:39+00:00Added an answer on June 17, 2026 at 9:33 pm

    I’m assuming you want the result of the first form to determine the number of fields and their labels of the second, you might want to look into Django form wizards. But here’s a simple, non-form-wizard (and probably less ideal/maintainable) way to do it, utilising the __init__ method of the formset to modify the form labels*:


    forms.py:

    # File: forms.py
    from django import forms
    from django.forms.formsets import BaseFormSet
    
    
    # What you've called 'GetMachine'
    class MachineForm(forms.Form):
        no_of_lines = forms.IntegerField(max_value=4)
    
    
    # What you've called 'GetLine'
    class LineForm(forms.Form):
        beamline_name = forms.CharField(max_length=15, label='Name of Beamline')
    
    
    # Create a custom formset and override __init__
    class BaseLineFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            super(BaseLineFormSet, self).__init__(*args, **kwargs)
            no_of_forms = len(self)
            for i in range(0, no_of_forms):
                self[i].fields['beamline_name'].label += "-%d" % (i + 1)
    

    views.py:

    # File: views.py
    from django.forms.formsets import formset_factory
    from django.shortcuts import render_to_response
    from django.template import RequestContext
    from django.http import HttpResponseRedirect
    from django.core.urlresolvers import reverse
    from forms import MachineForm, LineForm, BaseLineFormSet
    
    
    def get_no_of_lines(request):
        if request.method == 'POST':
            machine_form = MachineForm(request.POST)
            if machine_form.is_valid():
                # At this point, form fields have already been 
                # converted to Python data types :)
                # so no need to convert `line_no` to an integer
                no_of_lines = machine_form.cleaned_data['no_of_lines']
                return HttpResponseRedirect(reverse('line_form', kwargs={'no_of_lines': no_of_lines}))
        else:
            # It looks to me like you probably don't mean to
            # use formsets here (but a form instead)
            machine_form = MachineForm()
    
        c = RequestContext(request, {
            'machine_form': machine_form,
        })
        return render_to_response('get_no_of_lines.html', c)
    
    
    def line_form(request, no_of_lines):
        # You probably should validate this number (again).
        # In fact, you probably need to validate first form (MachineForm).
        # ...But I'm assuming it'll be valid in this example.
        no_of_lines = int(no_of_lines)
        LineFormSet = formset_factory(LineForm, extra=no_of_lines, formset=BaseLineFormSet)
        if request.method == "POST":
            formset = LineFormSet(request.POST, request.FILES)
            if formset.is_valid():
                pass
                # Do stuff with form submission
                # Redirect
    
        else:
            formset = LineFormSet()
    
        c = RequestContext(request, {
            'formset': formset,
        })
        return render_to_response('line_form.html', c)
    

    urls.py:

    from django.conf.urls import url, patterns
    from views import get_no_of_lines, line_form
    
    
    urlpatterns = patterns('',
         url(r'^$', get_no_of_lines, name='get_no_of_lines'),
         url(r'^line_form/(?P<no_of_lines>\d{1})$', line_form, name='line_form'),
    )
    

    get_no_of_lines.html:

    <form method="POST">
    {% csrf_token %}
    {{ machine_form }}
    </form>
    

    line_form.html:

    <form method="POST">
    {% csrf_token %}
    {{ formset.as_p }}
    

    The reason why I say this approach is probably not the best way to do this is because you have to validate no_of_lines being passed to line_form view (which could be > 4, so you’ll have to perform validation here and introduce validation logic rather than having it one place — the form). And if you need to add a new field to the first form, you’ll likely end up having to modify the code. So hence why I’d recommend looking into form wizards.


    • For more or dynamic forms, check out Jacob Kaplan-Moss’s post on dynamic form generation.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a formset within a formwizard. class Model1(models.Model): customerid = models.CharField(max_length=20)
ItemImageFormSet = inlineformset_factory(Item, ItemImage, extra=5, max_num=5) image_formset = ItemImageFormSet() I use the above code
use the [] symbol in the name of the form field you are submitting
I'd like to use the class based generic views of django 1.3 for forms,
how do you use model formset in Django? When you do this: from django.forms.models
Use case example: Client A comes to request sales information, enters their zip code
I'm trying to use a custom form, to populate over fields in another form,
From the source code, you can see that Django 1.4's Form class has a
I have activity records that have an inline formset showing appointments. I use the
I've looked at formset and model formset at Django many times, but I still

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.