简体   繁体   中英

How to create drop down in Django using the list in view

How can I create a dropdown in my template using the list values present in my view. I do not want this to be present in my models.py. My views.py has generated some values which I would like to display in dropdown.

Example: Say I have z=[1,2,3,4]. I would like this value to be displayed in a drop down box in the template.

in the view:

def your_handler(request):
    z = [1, 2, 3, 4]
    return render(request, 'yourapp/your_template.html', {'z': z})

in the template:

<label>values in z<label>
<select id="the-id">
    {% for i in z %}
    <option value="{{ i }}">{{ i }}</option>
    {% endfor %}
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM