On Tue, 2009-04-28 at 05:02 -0700, 83nini wrote:
> Hi,
> 
> I'm trying to display a form in the following way:
>  1. creating a form
>  2. writing a view method that looks like this:
>    def contact(request):
>     if request.method == 'POST':
>         form = ContactForm(request.POST)
>         if form.is_valid():
>             cd = form.cleaned_data
>             send_mail(
>                 cd['subject'],
>                 cd['message'],
>                 cd.get('email', 'nore...@example.com'),
>                 ['siteow...@example.com'],
>             )
>             return HttpResponseRedirect('/contact/thanks/')
>     else:
>         form = ContactForm(initial='subject')

This line is almost certainly incorrect and what will be causing the
problem later on. The "initial" parameter to a Form object's constructor
needs to a be a dictionary -- or something else satisfying the mapping
protocol -- since it provides initial data values for a number of form
field objects. Thus, it has to map the name of the form field to the
value.

You are providing only a string, which won't work. Since templates
intentionally swallow AttributeError (so that calling non-existent
methods fails, which is actually very useful in a number of situations),
you aren't seeing the error in the template.

Normal debugging practice for a problem such as you're seeing here would
be to reduce the number of variables involved. You have a view, a form
object *and* a template involved. The first thing to do when debugging
is remove as many of those as possible and verify each piece in
isolation. Firstly, from an interactive prompt, you check that the form
can be created and displayed. I suspect that will raise an exception and
points to the problem. If that worked, you would then test the template
by passing in just a hard-coded set of context variables that you know.
Then, if that worked, you could use test that the view behaved.
Debugging is all about simplifying things to the simplest possible case
-- it's pretty standard science practice in that respect.

Regards,
Malcolm



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

Reply via email to