[Answered ]-To retrieve values of multiple radio buttons and form fields in views.py django

2👍

you should better use django formsets https://docs.djangoproject.com/en/1.8/topics/forms/formsets/

As mentioned in documentation :

from django.forms.formsets import formset_factory
from django.shortcuts import render_to_response
from myapp.forms import ArticleForm

def manage_articles(request):
    ArticleFormSet = formset_factory(ArticleForm)
    if request.method == 'POST':
        formset = ArticleFormSet(request.POST, request.FILES)
        if formset.is_valid():
            for form in formset:
                form.cleaned_data...
                # here you can access each radio button


    else:
        formset = ArticleFormSet()
    return render_to_response('manage_articles.html', {'formset': formset})

The manage_articles.html template might look like this:

<form method="post" action="">
    {{ formset.management_form }}
    <table>
        {% for form in formset %}
        {{ form }}
        {% endfor %}
    </table>
</form>

Leave a comment