I have a form where I am editing single leads data, I submitted data using the Django default admin panel. But now I am editing data on my default form. So my concern is, if I edit any leads then the default value should be selected in the dropdown which is available in the database, and if I want to change the dropdown value then I can select it from the dropdown list.
Here are my Models.py file
class Leads(models.Model):
SOURCE = (
('Facebook', 'Facebook'),
('Website', 'Website'),
('PPC', 'PPC'),
('Reference', 'Reference'),
('Other Source', 'Other Source')
)
name = models.CharField(max_length=50, default=None)
email = models.EmailField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=10, default=None)
source = models.CharField(choices=SOURCE, default=2, max_length=100)
here are my urls.py file
path(r'^leads/<int:id>/change',views.leads_edit, name="leads_edit"),
here is my views.py file...
def leads_edit(request, id):
leads = Leads.objects.get(id=id)
context = {'leads':leads}
template_name = 'file/edit.html'
return render(request, template_name, context)
here is my edit.html file, where is am trying to edit using the dropdown field...
{% for obj in leads.SOURCE %}
<option value="{{ leads.source }}">{{ obj}}</option>
{% endfor %}
Please check my code and let me know how I can display the selected field from the database, and how I can display the rest other fields in the dropdown list. Because I can change the dropdown fields.