0

I'm trying to create a select in html to list all month and the default value of the select should be equal to the month parameter from the URL

My URL :

/list-working-session/?month=7&year=2019

month and year are the parameter

in My HTML:

<select name="month" class="form-control" id="month" required>
  {% for i in months %}
    {% if i == request.GET.month %}
      <option value="{{ i }}" selected="selected">{{ i }}</option>
    {% else %}
      <option value="{{ i }}">{{ i }}</option>
    {% endif %}
  {% endfor %}
</select>

months is the context from the view with range(1,13) and i managed to list from 1 to 12 in select option but i can't get the IF condition to work so the default select value equal to the month in the URL.

Any help would be appreciate

Saeed Alijani
  • 313
  • 2
  • 13
Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67

1 Answers1

1

request.GET.month is a string and is being compared with an integer. You will have to convert them to common type to get desired result

Please see Need to convert a string to int in a django template

Shirish Goyal
  • 510
  • 2
  • 9