template_values = {'Answer1':Answer1,
'Answer2':Answer2,
'Answer3':Answer3,
'Answer4':Answer4,
'Answer5':Answer5,
'QuestionText': QuestionText,
'address':address,}
path = os.path.join(os.path.dirname(__file__), 'QuestionPageOthers.html')
self.response.out.write(template.render(path, template_values))
HTML
<div><input type="RADIO" value={{Answer1}} name="answer" id="answer1"/>{{Answer1}}<span></span></div>
<div><input type="RADIO" value={{Answer2}} name="answer" id="answer2"/>{{Answer2}}<span></span></div>
<div><input type="RADIO" value={{Answer3}} name="answer" id="answer3"/>{{Answer3}}<span></span></div>
<div><input type="RADIO" value={{Answer4}} name="answer" id="answer4"/>{{Answer4}}<span></span></div>
<div><input type="RADIO" value={{Answer5}} name="answer" id="answer5"/>{{Answer5}}<span></span></div>
我正在进行一项调查,跟踪有多少用户选择不同的选项,如上面的代码,第一部分我将答案传递给Django模板,并使用单选按钮形成调查,但我如何检测哪个按钮是点击了。我需要跟踪每个按钮被选中的时间。但是,如何将结果传递回Python请求处理程序。 self.request.get("name")
适用于<input type="text">
,但不适用于单选按钮。
答案 0 :(得分:0)
如果您的单选按钮位于POST请求处理程序的表单中,那么您可以获取与所选单选按钮关联的值,如下所示:
select_answer = self.request.get('answer')
这非常类似于您检索为问题中描述的name
文本字段输入的值的方式 - 您只需将name
替换为与无线电输入元素关联的名称(即{{{ 1}}在这种情况下)。
答案 1 :(得分:0)
template.html
<form method="post" action="some-view">
<div><input type="RADIO" value={{Answer1}} name="answer" id="answer1"/>{{Answer1}}<span></span></div>
<div><input type="RADIO" value={{Answer2}} name="answer" id="answer2"/>{{Answer2}}<span></span></div>
<div><input type="RADIO" value={{Answer3}} name="answer" id="answer3"/>{{Answer3}}<span></span></div>
<div><input type="RADIO" value={{Answer4}} name="answer" id="answer4"/>{{Answer4}}<span></span></div>
<div><input type="RADIO" value={{Answer5}} name="answer" id="answer5"/>{{Answer5}}<span></span></div>
...
</form>
views.py
def some-view(request):
if request.method == 'POST':
answer = request.POST['answer']
# then update your tally here
...
上面的代码应该可行,但这不是我可以实现的方式。在这种情况下,您会发现使用django表单会更有效。