Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Django中发布后,如何从多个表单中获取值?_Django_Django Forms_Django Templates_Django Views - Fatal编程技术网

在Django中发布后,如何从多个表单中获取值?

在Django中发布后,如何从多个表单中获取值?,django,django-forms,django-templates,django-views,Django,Django Forms,Django Templates,Django Views,我在同一个html页面中为每一行选择了几个表单。假设选项为编辑、更新、添加,如果有5行,并且用户为其中两行选择更新,然后单击提交,我的视图将处理请求 要处理此请求,我需要一个选定选项列表: [{'row1':'UPDATE', 'row2': 'UPDATE'}] (okay... I need to be able to distinguish which choice belongs to which row...) 假设这是我的html文件 <table> <tr

我在同一个html页面中为每一行选择了几个表单。假设选项为编辑、更新、添加,如果有5行,并且用户为其中两行选择更新,然后单击提交,我的视图将处理请求

要处理此请求,我需要一个选定选项列表:

 [{'row1':'UPDATE', 'row2': 'UPDATE'}]   (okay... I need to be able to distinguish which choice belongs to which row...)
假设这是我的html文件

<table>
<tr>
   <td>{{ form.as_p}}</td>
   <td> 121 </td>
</tr>
   <td>{{ form.as_p}}</td>
   <td> 212 </td>
</table>

<form action='' method="POST">{% csrf_token %}
<input type="submit" name="Submit!"></input>
</form>
它给了我一个空白页,所以没有

如何从整个表及其关联数据(如行)中获取选定值的列表

多谢各位

最终代码

views.py

forms.py

hello.html


您的数据字段位于表单标记的外侧,这就是为什么您从POST中什么也得不到的原因。将数据字段放在表单标记内,如下所示:

<form action='' method="POST"><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='41f4aa9cc46e3e21bb46c99bc992973a' /></div>

<table>
<tr>
   <td>{{ form.as_p}}</td>
   <td> 121 </td>
</tr>
   <td>{{ form.as_p}}</td>
   <td> 212 </td>
</table>

<input type="submit" name="Submit!"></input>
</form>

您的数据字段位于表单标记的外侧,这就是为什么您从POST中什么也得不到的原因。将数据字段放在表单标记内,如下所示:

<form action='' method="POST"><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='41f4aa9cc46e3e21bb46c99bc992973a' /></div>

<table>
<tr>
   <td>{{ form.as_p}}</td>
   <td> 121 </td>
</tr>
   <td>{{ form.as_p}}</td>
   <td> 212 </td>
</table>

<input type="submit" name="Submit!"></input>
</form>
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from forms import MyForm
from django.core.context_processors import csrf 

def my_form(request):
    if request.method == "POST":
        form = MyForm(request.POST)
        e = request.POST.getlist("choice_field")

        return HttpResponse(e)
    else:
        form = MyForm()
    c = {'form':form}
    c.update(csrf(request))
    return render_to_response('hello.html', c)
from django import forms

CHOICES = (('value1', 'First',),('value2', 'Second',))

class MyForm(forms.Form):
    choice_field = forms.ChoiceField(choices=CHOICES)
<form action='' method="POST">{% csrf_token %}
<table>
<tr>
   <td>{{ form.as_p}}</td>
   <td> 121 </td>
</tr>
   <td>{{ form.as_p}}</td>
   <td> 212 </td>
</table>
<input type="submit" name="submit"></input>
</form>
<form action='' method="POST"><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='41f4aa9cc46e3e21bb46c99bc992973a' /></div>

<table>
<tr>
   <td>{{ form.as_p}}</td>
   <td> 121 </td>
</tr>
   <td>{{ form.as_p}}</td>
   <td> 212 </td>
</table>

<input type="submit" name="Submit!"></input>
</form>