Django Code Snippets

Powered by PythonAnywhere and Django 1.8.6

Django FormSet without JS

Date Added: Nov. 12, 2015, 3:57 p.m.

Author: craigderington



# 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

    
{% csrf_token %} {% crispy form %} {{ formset.management_form|crispy }}
{% for form in formset %} {% crispy form %} {% endfor %}

 

Tags: django, python

Return to List