开发者

how to store the input of radio into datastore

开发者 https://www.devze.com 2023-02-16 09:23 出处:网络
template_values = {\'Answer1\':Answer1, \'Answer2\':Answer2, \'Answer3\':Answer3, \'Answer4\':Answer4, \'Answer5\':Answer5,
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}}<spa开发者_开发百科n></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>

I am doing a survey which keeps track of how many users select different options like the codes above, the first part I pass the answers to the Django templates and form the survey using radio buttons, but how can I detect which button is clicked on. I need to keep track of times that each button has been selected. However, how can I pass the results back to the Python request handler. self.request.get("name") works for <input type="text"> but doesn't work for radio buttons.


If your radio buttons are located within the form being POST'ed to your request handler, then you can get the value associated with the selected radio button like this:

 select_answer = self.request.get('answer')

This quite similar to how you retrieved the value entered for the name text field you described in your question - you just replace name with the name associated with the radio input elements (i.e., answer in this case).


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
        ...

Above code should work, but it's not how i could have implemented this. You will find it more efficient to use django forms in this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消