Linh Nguyen
Linh Nguyen

Reputation: 3890

Django render range() between 2 number values as float instead of int in template

I'm trying to populate select option in HTML with range between 2019 and 2021

in my views.py:

return render(request, 'pages/list_working_sessions.html', {
        'working_session_list': working_session_list,
        'state': 'list-working-sessions',
        "months": range(1, 13),
        "years": range(2019, 2021)
    })

in my list_working_sessions.html:

                    <td style="border-top:none">
                        <select name="year" class="form-control" id="year" required>
                        {% for i in years %}
                            {% if i == request.GET.year|add:"0" %}
                                <option value="{{i}}" selected="selected">{{i}}</option>
                            {% else %}
                                <option value="{{i}}">{{i}}</option>
                            {% endif %}
                        {% endfor %}
                        </select>
                    </td>

the option only show 2.019 and 2.020 instead of 2019 and 2020.

but range(1,13) show properly int value

Why is this happening and how to fix this ? Thank you

Upvotes: 0

Views: 155

Answers (2)

Linh Nguyen
Linh Nguyen

Reputation: 3890

i found out why, in the project i'm working on i has auto format thousand number in settings

USE_THOUSAND_SEPARATOR=True

THOUSAND_SEPARATOR=','

DECIMAL_SEPARATOR='.'

NUMBER_GROUPING=3

so i just add |safe in template next to value to ignore the auto format

{{k|safe}}

Upvotes: 1

Matthew Schinckel
Matthew Schinckel

Reputation: 35619

It looks a bit like your numbers are being made "friendly". You could try using {{ i|stringformat:"s" }}.

However, I think you should avoid doing so much logic in your template: this particular type of processing is exactly what forms are for: you can even pass request.GET to a form and have the widget render the selected="selected" for you automatically.

Upvotes: 0

Related Questions