2

Im trying to create a simple form in django that takes a dropdown value and a text from the user. The text value entered by the user is passed and Im able to print it in the console. But the dropdown value is displayed as blank even after user selects a valid option. Can somebody please help me resolve it.

views.py

def product(request):
    if request.method == 'POST':
        form = ProductForm(request.POST)

        if form.is_valid():

            print ('selected dropdown value from the form is: ',form.cleaned_data['selection'])
            print('entered text is: ',form.cleaned_data['mytext'])
            #return render_to_response('loggedin.html',{'full_name' : request.user.username,'my_selection' :request.selection})
        else:
            print ('invalid selection')
            return render_to_response('loggedin.html',{'full_name' : request.user.username,'my_selection' : 'invalid'})
    else:
        form = ProductForm()

    args = {}
    args.update(csrf(request))
    args['form'] = ProductForm()
    print args
    return HttpResponseRedirect('/accounts/selected')

forms.py

class ProductForm(forms.Form):
    selection = forms.ChoiceField(required=False)
    mytext = forms.CharField(required=True)

loggedin.html

    {% extends "base.html" %}

{% block content %}

<h2> Hi {{full_name}} you are logged in!!</h2>
<form action="/accounts/product/" method="post">{% csrf_token %}
{{ form.errors }}
{{ form.non_field_errors }}
<label>Select a product:</label>
<select name="selection">
    <option selected="select" disabled>--select one--</option>>
    <option value="{{Iphone}}">Iphone</option>
    <option value="{{Samsung}}">Samsung</option>
    <option value="{{Motorola}}">Motorola</option>
    <option value="{{Blackberry}}">Blackberry</option>
    <option value="{{Nokia}}">Nokia</option>
</select>
<br/>
<label>Text: </label>
<input name="mytext" type="text"><br/>
<input type="submit" value="select">
</form>
<p>Click <a href="/accounts/logout/" here </a> to logout </p>
<p>Your selected product is: {{my_selection}}</p>
{% endblock %}

urls.py

url(r'^accounts/product/$', 'mysite.views.product'),
    url(r'^accounts/selected/$', 'mysite.views.selected'),
  • You should be using Django ChoiceField for this. See: http://stackoverflow.com/questions/24403075/django-choicefield – Dillon Apr 14 '15 at 18:42

3 Answers3

0

Why have you put all the values for the options within template variable symbols? You haven't defined any variables for Nokia, Blackberry etc, so those values will be rendered as empty strings. Just use normal strings:

<option value="Nokia">Nokia</option>
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

The variables {{Iphone}}, {{Nokia}} etc aren't defined (they supposed to be variables that comes from the view context when you call the render function). The value you get is a empty string, put static values on the html file, I suggest you to read more about templates and how to transfer variables from view to the template. Also, if you already created ProductForm use the choices option in ChoiceField.

Good luck!

Dor Atias
  • 1
  • 1
0

You can use jQuery to get the value selected in the dropdown and pass it by making an AJAX call.

<select id="products" name="selection">
   <option selected="select" disabled>--select one--</option>>
   <option value="iphone">Iphone</option>
   <option value="samsung">Samsung</option>
</select>

You can get the selected value using

var selected = $('#products').val();