Hello all,
I am new to django. I am trying to implement dropdown in the Form and stuck with it.I am not able to access the dropdown value in the HTML template. Please help me.
Models.py
RECYCLING_CHOICES = {
(“ENERGETIC”, “energetische Entsorgung”),
(“MATERIAL”, “stoffliche Entsorgung”),
(“DISPOSAL”, “Beseitigung”),
}
class WasteDetails(models.Model):
year = models.CharField(max_length=100)
waste_type = models.CharField(max_length=50)
disposer = models.CharField(max_length=50)
value = models.IntegerField(max_length=20)
recycling_method = models.CharField(
max_length=50, choices=RECYCLING_CHOICES, default=“DISPOSAL”
)
recycle_description = models.CharField(max_length=50)
Forms.py
class WastedataForm(forms.ModelForm):
class Meta:
model = WasteDetails
fields = [
“year”,
“waste_type”,
“disposer”,
“value”,
“recycling_method”,
“recycle_description”,
]
widget = forms.Select(choices=RECYCLING_CHOICES)
HTML template
{% extends ‘signupForm/base.html’ %}
{% block content %}
{% load static %}
Waste details
Please enter the details
{{form.waste_type}}
</div>
<div class="col-md-12">
<label for="disposer" class="form-label">Disposer</label>
{{form.disposer}}
</div>
<div class="col-md-12">
<label for="value" class="form-label">Value</label>
{{form.value}}
</div>
<div class="col-md-12">
<label for="cfactor" class="form-label">Calculationfactor</label>
{{form.calculation_factor}}
</div>
<div class="col-md-12">
<label for="recyclemethod" class="form-label">Recyclingmethod</label>
<select id="recyclemethod" class="form-select" aria-label="Default select example">
<option selected>Choose</option>
{% for recycle in recycling_method %}
<option value="{{recycle.recycle_method}}">{{recycle.choices}}</option>
{% endfor %}
</select>
</div>
<div class="col-md-12">
<label for="description" class="form-label">Recyclingdescription</label>
{{form.recycling_description}}
</div>
<div class="col-12 pt-3 pb-3">
<button type="submit" class="btn registerbtn mt-2">Submit</button>
</div>
</form>
</div>
<div class="col-md-6">
<img src="{% static 'signupForm/img/sidepic.jpg' %}"
alt="responsive-img" class="img-fluid rightpic"/>
</div>