Reading user selection from Django 1.5 template without using Models
<p>Im using <strong>Python27</strong> with <strong>Django 1.5</strong>.</p>

<p>i have been scowering the internet for hours without success. Is there a way to capture what the user has selected from a dropdown listbox in a template without using Models? Im looking for a direct way to read some sort of var into code in views.py.</p>

<p><em>example.html</em></p>

<pre><code>&lt;select name="num_select"&gt;
    &lt;option value="0"&gt;-----&lt;/option&gt;
    &lt;option value="10"&gt;1 - 10&lt;/option&gt;
    &lt;option value="20"&gt;10 - 20&lt;/option&gt;
    &lt;option value="30"&gt;20 - 30&lt;/option&gt;
    &lt;option value="40"&gt;30 - 40&lt;/option&gt;
    &lt;option value="all"&gt;all&lt;/option&gt;
&lt;/select&gt;
</code></pre>

<p>which every option i select I want to pass the value to a var in my views.py</p>

<p><em>views.py</em></p>

<pre><code>def my_view(request):
    ...
    num_select = forms.????.get['num_select']
    ...
    return blah blah....
</code></pre>

<p>I hope i have provided enough details for you to assist me. Not sure what else i can add. I have been going through pages of docs without success. I know how to get vars from views.py to html templates, but not vise-versa. </p>

<p>Help is greatly appreciated. </p>

<p>Yes, you don't need models for this at all, what you need is a simple <a href="https://docs.djangoproject.com/en/1.10/topics/forms/" rel="nofollow">django form</a>. </p>

<pre><code>CHOICES = ( (1,"10"), (2,"20"), ...)

class MyForm(forms.form):
    ...
    num_select = forms.ChoiceField(choices = CHOICES)
    ...
</code></pre>

<p>Then in your view</p>

<pre><code>from myapp.forms import MyForm

def my_view(request):

    if request.method = 'POST':
        form = MyForm(request.POST)
        if form.is_valid():
              num_select = form.cleaned_data['num_select']
</code></pre>

<p>This is of course a rather stripped down version of it, for additional information refer to the link above.</p>

<p>If you don't want to use a form either, you can just access <code>request.POST</code> directly, but then what's the point of using Django? you might as well use PHP</p>

python html django templates html-select