[Answer]-Drop down menu from Django database

1👍

For example – you have a cars list in Template context. You can do something like this, if I understand you correctly:

<form>
<select name="cars">
  {% for car in cars%}
     <option value={{ car.name }}>{{ car.name }}</option>
  {% endfor %}
</select>
<input type="submit" value="Submit">
</form>

0👍

Describe your model and your form as well. It’s pretty simple in Django. If you have a form and the form is passed to the template, you can render the form as follows:

{{ form.as_p }}

Model example:

from django.db import models

# Your model choices
class SampleCategory(models.Model):
    title = models.CharField(max_length=255)

# Your sample model that uses the model with choices
class SampleModel(models.Model):
   category = models.ForeignKey(SampleCategory)

Form example:

from django import forms

from yourapp.models import SampleModel

class SampleModelForm(forms.ModelForm):
    class Meta:
        model = SampleModel

Leave a comment