List Info

Thread: retaining the data on validation error using newforms




retaining the data on validation error using newforms
country flaguser name
United States
2007-07-30 20:59:57
Hi,

this is how I am using the newforms.

My Form

class NewEmployeeForm(forms.Form):

    contract_from = forms.DateField()
    contract_to = forms.DateField()
    position = forms.CharField(max_length=20)

My View

def create_employee(request):
    f = None
    if request.method == 'POST':
        f = NewEmployeeForm(request.POST)
        if f.is_valid():
            return HttpResponse('valid')

    return render_to_response('create_employee.htm',
{'form':f})

My Template

<form method="post"
action="/manning/create_employee/">
		<p>
			<label for="id_contract_from">Contract
from:</label>
			<input type="text"
name="contract_from"
id="id_contract_from" />
			{{form.contract_from.errors}}
		</p>
		<p>
			<label for="id_contract_to">Contract
to:</label>
			<input type="text"
name="contract_to" id="id_contract_to"
/>
			{{form.contract_to.errors}}
		</p>
		<p>
			<label
for="id_position">Position:</label>
			<input id="id_position"
type="text" name="position"
maxlength="20" /
>
			{{form.position.errors}}
		</p>
		<p>
			<input type="submit"
value="Create">
		</p>
		</form>

my problem is after the validation error, I would like to
populate the
form with the posted data. One solution is to change my view
to

def create_employee(request):
    if request.method == 'GET':
        f = NewEmployeeForm()
    if request.method == 'POST':
        f = NewEmployeeForm(request.POST)
        if f.is_valid():
            return HttpResponse('valid')

    return render_to_response('create_employee.htm',
{'form':f})

and my template to
<form method="post"
action="/manning/create_employee/">
		<p>
			{{form.contract_from.label}}
			{{form.contract_from}}
			{{form.contract_from.errors}}
		</p>
		<p>
			{{form.contract_to.label}}
			{{form.contract_to}}
			{{form.contract_to.errors}}
		</p>
		<p>
			{{form.position.label}}
			{{form.position}}
			{{form.position.errors}}
		</p>
		<p>
			<input type="submit"
value="Create">
		</p>
		</form>

but my problem here is that how can I apply my css if I
can't change
the class attribute of label and input tag generated by the
newforms?

any ideas?

james


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: retaining the data on validation error using newforms
country flaguser name
United States
2007-07-31 01:26:28
Everything can be changed.  Look under auto_id in the
newforms docs on
djangoproject.com.  The default does it exactly the way you
seem to
be, prepending 'id_' to the field name.  If you want to set
a class,
you need to change the attrs dict on the field's widget.

All a newforms field really consists of is a field class
that handles
validation, and a widget class that handles making the html
form
element for that field.  By defining your form class they
start out in
FormClassName.base_fields['fieldname'].  Once you actually
make a form
instance by doing form=FormClassName(), you access them via
form.fields['fieldname'] and not base_fields (because the
Form class
is a metaclass).  So, if you have a form instance:

form.fields is a dict of all the form fields. 
form['fieldname'] is
the field class instance for the type you gave fieldname
when you
defined the form.  form['fieldname'].widget is the widget
class
instance which you either specified, or django supplied the
default
widget for the field type. The widget has an attribute dict
called
'attrs', that is home for the form element attributes like
'class', or
'size', or 'style'.  If you put them in the attrs dict, they
show up
on the html element when it's rendered in the template.

Three different ways of doing the exact same thing (adding a
css class
to a field):

You can do in the form class when you define the form,
contract_from =
forms.DateField(widget=TextInput(attrs={'class':'cssclassnam
e'}))
or
After the form as been defined, but not instantiated by
modifying
base_fields
NewEmployeeForm.base_fields['contract_from'].widget.attrs['c
lass'] =
'cssclassname'
or
After the form has been instantiated by modifiying for forms
local
field definitions, 'fields'
form.fields['contract_from'].widget.attrs['class'] =
'cssclassname'


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )