开发者

how can I bind values to form inputs Django

开发者 https://www.devze.com 2023-04-09 03:11 出处:网络
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

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消