Coco
Coco

Reputation: 39

How to get multiple values selected from the drop down to views.py

Am using the following code:

index.html

<div class="col-sm-3" style="margin-left: 30px;">
            <select id="month" name="month" multiple>

                  <option value="01">January</option>
                  <option value="02">February</option>
                  <option value="03">March</option>
                  <option value="04">April</option>
                  <option value="05">May</option>
                  <option value="06">June</option>
                  <option value="07">July</option>
                  <option value="08">August</option>
                  <option value="09">September</option>
                  <option value="10">October</option>
                  <option value="11">November</option>
                  <option value="12">December</option>


            </select>
          </div>

Views.py

def internal(request):
   try:
       year = ''
       month = []
       year = request.GET.get('year')
       month = request.GET.get('month')
       print(month)
       response_list = []
   except Exception as e:
       print(str(e))
   return HttpResponse(json.dumps(response_list))

I am selecting more than one value in the front-end but when I fetch it in views.py only one options is getting fetched. How do I fetch all the values selected from dropdown?

Upvotes: 2

Views: 1002

Answers (1)

Lomtrur
Lomtrur

Reputation: 1810

You need to use getlist(key).

Like this:

month = request.GET.getlist('month')

get(key) will get you only the last selected value:

If the key has more than one value, it returns the last value.

Upvotes: 3

Related Questions