3

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.

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

3 Comments

glat it helped.
@alfonso.kim . hey can tell how to get the selected value of the dropdown in view function in above question. for eg. if I choose 3 then how can I get this value in a function on view.py page without using any Django models and forms
@girijesh96 check the docs about request and response objects

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.