在表单字段中显示默认值 Django

hln*_*hln 4 python django django-forms

django 如何在要形成的文本字段中显示默认值。

<input type="text" name="{{ form.username}}" value="{{ costumer.username}}"><p>
Run Code Online (Sandbox Code Playgroud)

它在浏览器中显示一个文本字段,后跟costumer.username,我想在文本字段中使用用户名作为默认值,我该怎么做?

ndp*_*dpu 6

对表单使用初始参数:

form = Form(initial={'username': costumer.username})
Run Code Online (Sandbox Code Playgroud)

并在模板中显示输入,您只需要:

{{ form.username }}<br/>
Run Code Online (Sandbox Code Playgroud)

https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values