1

I have a HTML form

<form>
    <select id="lr_action" name="lr_action">
        <option value="CENTER">Center</option>
        <option value="LEFT">Left</option>
        <option value="RIGHT">Right</option>
    </select>
</form>

Is there a way I can receive and set the value of this 'select' dropdown in views.py (where I load my main HTML page) without using Django forms class. I am looking for a easy equivalent for the js attribute 'getElementById'.

Thank you

0

1 Answer 1

3

In your view, get the value from request.GET (the default method of an html form). Pass the submitted value to your template via context.

lr_value = request.GET.get('lr_select',None)
# your template method, whatever it is
return render_to_response('template.html',{'lr_value':lr_value},RequestContext(request))

Then in your template you would have to check per option.

<option value="CENTER"{% if lr_value == "CENTER" %} selected{% endif %}>Center</option>
<option value="LEFT"{% if lr_value == "LEFT" %} selected{% endif %}>Left</option>
<option value="RIGHT"{% if lr_value == "RIGHT" %} selected{% endif %}>Right</option>

If you are doing this to take shortcuts, I'd advise against it. Django Forms take some getting used to but are extremely powerful and concise to write when used properly. Nonetheless, the method above will work for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.