检索views.py django中多个单选按钮和表单字段的值

时间:2021-10-29 13:45:50

i want to receive the radio button values for the table generated within the form multiple times in views.py in django.

我想在django中的views.py中多次接收表单中生成的表格的单选按钮值。

My templates file is something like :

我的模板文件类似于:

<form action = "/appName/accept" method = "POST"
    {% for l in list %}
        <table>
        <tr>
        <td>{{l.value}}</td> 
        </>tr
        </table>  
        <input type = "radio" name = "acceptance" value ="accept">Accept</input>
        <input type = "radio" name = "acceptance" value ="reject">Reject</input>
     {% endfor %}
     <input type ="submit" name = "submit">SUBMIT</input>
</form>

that means rendered page will look like :

这意味着渲染页面将如下所示:

value 1

价值1

  • Accept

    接受

  • Reject

    拒绝

value 2

价值2

  • Accept
  • 接受
  • Reject
  • 拒绝

and so on SUBMIT BUTTON

提交按钮等等

as soon as user clicks the submit button, I want to collect the values of all the radio buttons for each value as well as the table rows but how to do that in views.py? I know that if we give common name to radio input like here "acceptance", so only of one of them will be selected at a time. Please Help. I am new to this concept and django.

一旦用户单击提交按钮,我想收集每个值以及表行的所有单选按钮的值,但是如何在views.py中执行此操作?我知道如果我们给这个“接受”这样的无线电输入命名,那么一次只能选择其中一个。请帮忙。我是这个概念和django的新手。

1 个解决方案

#1


1  

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

你应该更好地使用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:

manage_articles.html模板可能如下所示:

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

#1


1  

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

你应该更好地使用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:

manage_articles.html模板可能如下所示:

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