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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

i see that why it doesn't work. i changed by using add filter after the value request.GET.month|add:"0" and it worked
Did you also try using int(request.GET.month) instead?
int() can't be use in template, i tried it and got the error Could not parse the remainder: '(request.GET.month)' from 'int(request.GET.month)'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.