how can I bind values to form inputs Django
I am trying to do an edit page where I fill the input boxes with the values taken from the database.
Say I have a model Employees
and a field name
, a form EmployeeForm
and an input field name
emp_info = Employees.objects.get(pk=1)
emp_form = EmployeeForm()
I am currently doing this and it does not seem to work
emp_form.开发者_StackOverflow中文版name = emp_info.name
template:
{{ emp_form.name }}
What is the correct way to do it?
If you can't use a model form for some reason, try this
emp_info = Employees.objects.get(pk=1)
emp_form = EmployeeForm(initial={'name': emp_info.name})
emp_info = Employees.objects.get(pk=1)
This will populate your form with the emp_info
.
emp_form = EmployeeForm(instance=emp_info)
精彩评论