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
- [Answer]-Select_related django. Why i can't using select_related in my project?
- [Answer]-UWSGI process doesn't inherit permissions associated with group its uid belongs to
- [Answer]-Django Template not rendering correctly
- [Answer]-Getting invalid syntax while creating new app in django
Source:stackexchange.com