关于python:如果页面上有多个表单而没有提交按钮Django 1.10,如何传递表单ID?

How to pass form id if there are several forms on page and no submit buttons Django 1.10?

我有一页问题清单。他们每个人都有带有"是"、"可能"、"否"选项的单选按钮窗体。通过选择提交此表单。网址:http://prntscr.com/embo1m

我不明白如何把问题ID和它的形式联系起来。如何将其置于有响应的视图中?

型号.py

1
2
3
4
5
6
7
8
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)

视图.py

1
2
3
4
5
6
7
8
9
10
11
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)

表格.py

1
2
3
4
5
6
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')

模板

1
2
3
4
5
6
7
8
9
10
11
{{ apartment.title }}
{% for question in questions %}
{{ question.title }}
<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 %}

。编辑:

试图以的身份传递问题id

1
2
3
4
5
6
7
8
9
10
11
{{ apartment.title }}
{% for question in questions %}
{{ question.title }}
    <form id="question_{{ question.id }}" name="question_{{ question.id }}" action="/apartments/{{ apartment.id }}/" method="post">
        {% csrf_token %}
        <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 %}
    </form>
{% endfor %}

把我的表格改成

1
2
class AnswerForm(Form):
    choice = ChoiceField(label='', widget=RadioSelect, choices=ANSWERS)

但是在表单中没有来自响应的隐藏输入…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<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作为隐藏值传递,如下所示:

1
2
3
4
<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 %}