[Answered ]-Get the id and description from database for dropdown in django

1👍

Judging from the answer to my comment, you are a complete django beginner. Therefore, I recommend starting with the docs.

I will provide you with some guidance nevertheless. First, you write yourself a view similar to this (note that this is very minimalistic):

def product_view(request):
    context = { 'products': Products.objects.all() }
    return render(request, 'products/product_view.html', context)

Then, in that product_view.html template, you can create dropdowns like this:

{% if products %}
  <label for="products">Choose a product:</label>

  <select name="cars" id="cars">
  {% for product in products %}
    <option value="{{ product.pk }}">{{ product.name }} -- {{ product.number}}</option>
  {% endfor %}
  </select>
{% endif %}

👤dacx

Leave a comment