views:

631

answers:

2

Hi stack overflow,

I'm trying to style a form with CSS. First of all I haven't seen a complete example, other than on the official documentation, so I would appreciate any blog entries, or articles.

Now on my form, a typical Charfield gets translated on html like this: input type="text" name="artists" id="id_artists"

If my form contains errors on some field, the previous Charfield remembers the value and goes:

input type="text" name="artists" value="Pink Floyd" id="id_artists"

How can I get this value (value="Pink Floyd") in django forms? Say my field is {{form.artists}}, I can use {{form.artists}} as the field, {{form.artists.label}} as label, {{form.artists.errors}} and {{form.artists.help_text}}, but how about this value?

Thanks in advance!

+2  A: 

Create the input field specifically, rather than relying on django to auto-create it.

Something like:

<input type="text" name="artists" id="id_artists" value="{{form.artists.title}}" />

should work

Steerpike
Hi Steerpike,this will do the job, however I still need to get that value, in order to provide it when there are errors on some other field on the form - otherwise it gets blanked.
Markos Gogoulos
+1  A: 

You can get the current value of the field from the data property:

{{ form.artists.data }}

I can't see the Django documentation mentioning this, but it works...

Guðmundur H