0

views.py

def select_value(request):
    value = request.GET['value']
    template = 'apps/selected_value.html'
    return render(request, template, {'values': value})

<select name="value">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>
<p>you have selected value {{values}}</p>

i am new to Django and i did lots of searching and for get the selected value from html to django views. i tried with above html and django viwes, still i am getting below. can any one help me on this? thanks in advance.

getting MultiValueDictKeyErrorat /value/ 'value'

Venkat
  • 3
  • 5
  • I'm suspecting that something is wrong with the way you get your values. More on [here](https://stackoverflow.com/a/5895670). – crimsonpython24 Jul 21 '20 at 08:31
  • Please give details about how you send that value from – Smit Patel Jul 21 '20 at 08:39
  • from this code snippet, it looks that you are rendering the template, but not sending selected value back to server. there must me a form or ajax code to do that. – Smit Patel Jul 21 '20 at 08:40

1 Answers1

0

First of all Do a little searching about 'Post' vs 'GET', TO know when to use each one. I recommend you to handle this with form.

    .Html file 
    <form action="PageObjects" method="post">
      <select >
        <option selected="selected" disabled>Objects on page:</option>
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="30">30</option>
        <option value="40">40</option>
        <option value="50">50</option>
      </select>
      <input type="submit" value="Select">
    </form>

view.py
def page_objects(request):
  if request.method == 'POST':
    form = YourForm(request.POST)

    if form.is_valid():
      answer = form.cleaned_data['value']
Archiee
  • 85
  • 9