Search code examples
formswebrequestpythonanywhere

Request.form in pythonanywhere


I think I might be using request.form incorrectly on Pythonanywhere. I have several dropdowns, which look like the following in the html:

<span class="text" style="margin-left: 4px;">{{form.q1Text}}</span><br>
<select name="q1a" class="question">
{% for p in form.q1Opts %}
    <option value={{p}}>{{p}}</option>
{% endfor %}
</select>

I pass in a form, which looks like this:

class questionForm(Form):
    q1Text = "This is a question?"
    q1Opts = ["Option A","Option B","Option C","Option D"]

Once the form is submitted, I want to be able to access the option the person selected, so I do the following:

answer = request.form["q1a"]

In this particular example, I feel I should be getting "Option A" back from the form. The problem is that I don't. I get back "Option". This is true no matter what I put in as options; only the first word (of the one the user selected) is returned. I have been working around this problem for some time now, but I'm hoping someone actually knows how to fix it and get me the whole string back.

Oh, I feel I should point out that the options do display properly on the web page. So the user sees "Option A", "Option B" and so on, but when I try and read their response, I get only whatever the first word is.


Solution

  • It looks like you're missing the quotes around the value= attribute?

    It should be:

    <option value="{{p}}">{{p}}</option>