tags:

views:

360

answers:

1

I am trying to set the field to a certain value after the form is initialized.

For example, I have the following class.

class CustomForm(forms.Form):
    Email = forms.EmailField(min_length=1, max_length=200)

In the view, I want to be able to do something like this:

form = CustomForm()
form["Email"] = GetEmailString()

return HttpResponse(t.render(c))
+6  A: 

Since you're not passing in POST data, I'll assume that what you are trying to do is set an initial value that will be displayed in the form. The way you do this is with the initial keyword.

form = CustomForm(initial={'Email': GetEmailString()})

See the Django Form docs for more explanation.

If you are trying to change a value after the form was submitted, you can use something like:

if form.is_valid():
    form.cleaned_data['Email'] = GetEmailString()

Check the referenced docs above for more on using cleaned_data

Grant
That is exactly what I want to do. Thanks!
Eldila