On Jul 29, 9:17 pm, When ideas fail <andrewkenyon...@gmail.com> wrote: > I've created a model form and I was wondering what the correct way to > save this was. I've tried adapting the view from the standard django > forms docs but maybe it should be different? > > This is what i have in my view (it says contact form but its a form > that should save contacts in a db): > > class ContactForm(ModelForm): > class Meta: > model = Contact > > def contact_view(request): > if request.method == 'Post': > form = ContactForm(request.POST) > if form.is_valid(): > contact= form.save() > return HttpResponseRedirect('/thanks/') > else: return HttpResponseRedirect('/error/') > > else: > form = ContactForm() > tags = Tag.objects.all() > return render_to_response('Site/contact.html', locals(), > context_instance=RequestContext(request)) > > And my template is like this: > > <form method="POST"> > {{ form.as_p }} > <input type="submit" value="Submit" /> > > My understanding is that with this setup it should go to thanks/ if > the form is valid and error/ if it is not and it doesn't do either, > and it doesn't save or come back with any errors either . could > someone help me please? > > Thanks
There are two problems, but the main one is in the first line of your view. request.method == 'Post' will *never* be true, because the method (as in shown in your template) is all upper case. request.method == 'POST' should work. Secondly, don't redirect to /error/. Drop that else clause altogether, and let the view fall through to the final render_to_response. This will redisplay the form with any errors. -- DR. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---