Login

Dynamic formset without javascript

Author:
axelwass
Posted:
August 25, 2015
Language:
Python
Version:
1.7
Score:
0 (after 0 ratings)

If using javascript is not an option, you can use something like this code to have a variable number of subforms.

This code uses crispy-forms, but it is totally dispensable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# forms.py

class FormsetForm(forms.Form):
    delete= forms.BooleanField(required=False, initial=False)
    # some other fields with data

# views.py

def some_view(request):
    if request.method == 'POST':
        if request.POST['action'] == "+":
            extra = int(float(request.POST['extra'])) + 1
            form = SpecificForm(initial=request.POST)
            formset = formset_factory(FormsetForm, extra=extra)
        else:
            extra = int(float(request.POST['extra']))
            form = SpecificForm(request.POST)
            formset = formset_factory(FormsetForm, extra=extra)(request.POST)

            if form.is_valid() and formset.is_valid():
                if request.POST['action'] == "Create":
                    for form_c in formset:
                        if not form_c.cleaned_data['delete']:
                            # create data
                elif request.POST['action'] == "Edit":
                    for form_c in formset:
                        if form_c.cleaned_data['delete']:
                            # delete data
                        else:
                            # create data
                return HttpResponseRedirect('abm_usuarios')
    form = SpecificForm()
    extra = 1
    formset = formset_factory(FormsetForm, extra=extra)

    template = loader.get_template('some_template.html')
    context = RequestContext(request, {
       # some context
    })
    return HttpResponse(template.render(context))

# some_template.html

    <form method="post" action="{{ action }}" enctype="multipart/form-data">
        {% csrf_token %}
            {% crispy form %}
            {{ formset.management_form|crispy }}
            <input type="hidden" name="extra" value="{{extra}}">
            <fieldset>
                {% for form in formset %}
                    {% crispy form %}
                {% endfor %}
                <input type="submit" name="action" value="+">
            </fieldset>
        <div style="text-align: right">
        {% if not edit%}
            <input type="submit" name="action" value="Create">
        {% else %}
            <input type="submit" name="action" value="Edit">
        {% endif %}
        </div>
    </form>

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

barkalez (on May 11, 2019):

IndentationError: expected an indented block

#

Please login first before commenting.