Python 如果页面上有多个表单并且没有提交按钮Django 1.10,如何传递表单id?
我有一个问题列表页面。每一个都有单选按钮,上面有“是”、“可能”、“否”等选项。通过选择提交此表单。 我不明白如何将问题id与其表单连接起来。如何在它的视图中获得响应 models.pyPython 如果页面上有多个表单并且没有提交按钮Django 1.10,如何传递表单id?,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我有一个问题列表页面。每一个都有单选按钮,上面有“是”、“可能”、“否”等选项。通过选择提交此表单。 我不明白如何将问题id与其表单连接起来。如何在它的视图中获得响应 models.py class Answer(models.Model): user = models.ForeignKey(User) apartment = models.ForeignKey(Apartment) question = models.ForeignKey(Question)
class Answer(models.Model):
user = models.ForeignKey(User)
apartment = models.ForeignKey(Apartment)
question = models.ForeignKey(Question)
choice = models.IntegerField(default=2)
def __str__(self):
return str(self.choice)
views.py
def questions_list(request, apartment_pk):
context = {}
context['apartment'] = get_object_or_404(Apartment, pk=apartment_pk)
context['questions'] = get_list_or_404(Question)
context['answer_form'] = AnswerForm
context['username'] = auth.get_user(request).username
if request.method == 'POST':
form = AnswerForm(request.POST or None)
if form.is_valid():
print(form.cleaned_data)
return render(request, 'main/questions.html', context)
forms.py
class AnswerForm(ModelForm):
choice = ChoiceField(label='', widget=RadioSelect, choices=ANSWERS)
question_id = CharField(widget=HiddenInput(), required=False)
class Meta:
model = Answer
fields = ('choice', 'question_id')
模板
<h1>{{ apartment.title }}</h1>
{% for question in questions %}
<h3>{{ question.title }}</h3>
<form id="question_{{ question.id }}" name="question_{{ question.id }}" action="/apartments/{{ apartment.id }}/" method="post">
{% csrf_token %}
{% for radio in answer_form.choice %}
<label><input name="choice" type="radio" value="{{ radio.choice_value }}" onchange="question_{{ question.id }}.submit()">{{ radio.choice_label }}</label>
{% endfor %}
{{ answer_form.hidden_field }}
</form>
{% endfor %}
但在形式上没有来自响应的隐藏输入
<tr><th></th><td><ul id="id_choice">
<li><label for="id_choice_0"><input id="id_choice_0" name="choice" type="radio" value="0" required /> Точно ні</label></li>
<li><label for="id_choice_1"><input id="id_choice_1" name="choice" type="radio" value="1" required /> Скоріше ні</label></li>
<li><label for="id_choice_2"><input checked="checked" id="id_choice_2" name="choice" type="radio" value="2" required /> Не можу відповісти</label></li>
<li><label for="id_choice_3"><input id="id_choice_3" name="choice" type="radio" value="3" required /> Скоріше так</label></li>
<li><label for="id_choice_4"><input id="id_choice_4" name="choice" type="radio" value="4" required /> Точно так</label></li>
</ul></td></tr>
ччі
ССцііі
жжіііі
Саааа
а
清理的数据
{'choice':'2'}
将问题id作为隐藏值传递,如下所示:
<input type="hidden" name="q_id" value="{{ question.id }}">
{% for radio in answer_form.choice %}
<label><input name="choice" type="radio" value="{{ radio.choice_value }}" onchange="question_{{ question.id }}.submit()">{{ radio.choice_label }}</label>
{% endfor %}
{应答形式为无线电的%choice%}
{{radio.choice_label}
{%endfor%}
是否希望在表单提交后重新加载页面?仅供参考HTMLid
s不能只有数字。如果不需要JS,可以将其更改为类似以下内容的内容formid=“form-{{{question.id}}”@abijithmgno。另一方面,重新加载是可以接受的。如果不使用JS提交适当的表单,这是非常困难的。@nik_m将其更改为form id=“question”{{question.id}
我没有得到这个隐藏的输入作为回应:- <如果你能够接收单选按钮值,你应该得到它。正如你所看到的,我从表单
question\u id=CharField(widget=HiddenInput(),required=False)
中得到
。但我也试着用表单重写表单,而不是模型表单。而且我根本没有得到隐藏的信息。。。。
<tr><th></th><td><ul id="id_choice">
<li><label for="id_choice_0"><input id="id_choice_0" name="choice" type="radio" value="0" required /> Точно ні</label></li>
<li><label for="id_choice_1"><input id="id_choice_1" name="choice" type="radio" value="1" required /> Скоріше ні</label></li>
<li><label for="id_choice_2"><input checked="checked" id="id_choice_2" name="choice" type="radio" value="2" required /> Не можу відповісти</label></li>
<li><label for="id_choice_3"><input id="id_choice_3" name="choice" type="radio" value="3" required /> Скоріше так</label></li>
<li><label for="id_choice_4"><input id="id_choice_4" name="choice" type="radio" value="4" required /> Точно так</label></li>
</ul></td></tr>
<input type="hidden" name="q_id" value="{{ question.id }}">
{% for radio in answer_form.choice %}
<label><input name="choice" type="radio" value="{{ radio.choice_value }}" onchange="question_{{ question.id }}.submit()">{{ radio.choice_label }}</label>
{% endfor %}