Abhijit
Abhijit

Reputation: 1856

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.

Upvotes: 3

Views: 5252

Answers (1)

alfonso.kim
alfonso.kim

Reputation: 2900

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>

Upvotes: 6

Related Questions