hln
hln

Reputation: 1106

Display a default value in form fields Django

How does django display the default value in a textfield to form.

<input type="text" name="{{ form.username}}" value="{{ costumer.username}}"><p>

it shows a textfield follow by costumer.username in browser, I want to have the username as default value in the textfield, How can i do that?

Upvotes: 5

Views: 8577

Answers (2)

karthikr
karthikr

Reputation: 99620

In your view:

myForm = MyForm(initial={'username': costumer.username })

and in the template:

{{myForm.username|safe}}

Should do the trick.

Upvotes: 2

ndpu
ndpu

Reputation: 22561

Use initial parameter to a form:

form = Form(initial={'username': costumer.username})

and to display input in template you need just this:

{{ form.username }}<br/>

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

Upvotes: 7

Related Questions