def post(self, request):
form = AnswerForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
messages.success(request, 'Thank you for your request')
return redirect('consumer_answer')
args = {'form':form}
return render(request, self.template_name, args)
class AnswerForm(forms.ModelForm):
question1 = forms.ModelChoiceField(queryset = Question.objects.all())
class Meta:
model = Answer
fields = ['answer', 'consumer']
{% for answers in answer_request %}
<p>______________________________________________________________</p>
<b><li> {{answers.5}}</li></b>
<i><p> {{answers.6}}</p></i>
Question ID: {{answers.8}}<br>
<form method="post">
{% csrf_token %}
{{form.as_p}}
<select id="question1">
<option value= {{ answers.8 }}> Label </option>
</select>
<button type="submit">Submit Answer {{answers.8}}</button>
</form>
{% endfor %}
</ol>
{% endif %}
目前,上面的代码创建了一个空白的字段,称为“ answer”(在用户完成后进行细化,然后将其发布到db,但是接下来的“ consumer”和“ question”两个字段是模型中的所有实例)。希望“消费者”和“问题”字段对于查询结果不可编辑,因此将它们发布到数据库中。该查询在窗体外部运行良好,因为窗体上方的循环成功迭代并显示了输出,我只想将其放入窗体中。预先感谢您的帮助。
答案 0 :(得分:0)
本人已解决以下问题:
q = str(answer_request [1] [0]) form.fields [“ question”]。queryset = Question.objects.filter(question_id = q)