Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 521
hln Avatar asked Apr 19 '13 14:04

hln


2 Answers

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

like image 189
ndpu Avatar answered Sep 30 '22 12:09

ndpu


In your view:

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

and in the template:

{{myForm.username|safe}}

Should do the trick.

like image 27
karthikr Avatar answered Sep 30 '22 14:09

karthikr