On Jul 3, 8:57 am, 10000angrycats <[email protected]>
wrote:
> Great suggestion Rajesh.
>
> I've shifted everything into a custom context processor and
> everythign's runnign well, with the view showing and emailing from any
> page, however there's a final step I'm looking to resolve: returning
> the HttpResponseRedirect within the context processor fails silently.
>
> The context_processor.py file is this:
>
> def login_form(request):
> from website.contact.forms import ContactForm
> from django.core.mail import send_mail
> from django.http import HttpResponseRedirect
> 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', '[email protected]'),
> ['[email protected]'],
> )
> return HttpResponseRedirect('/contact/thanks/')#Not
> redirecting
> yet.
> else:
> form = ContactForm(
> initial={'subject': 'Here's a suggestion...'}
> )
> return {'form': form}
>
> There's little documentation I've found on this one. Any help
> appreciated.
You need to break that into two pieces because the context processor
can not handle submissions (i.e. your POSTed data).
1. Leave the "def contact(request)" view from your original post as
is. It should be able to handle posted data.
2. In your contact processor just return the initial form i.e. the
part of the "else" like this:
form = ContactForm(initial={'subject': 'Here's a suggestion...'})
return {'form': form}
In the template tag that renders the form, change the action attribute
to post to the contact view from #1 above.
In other words, you are using the context processor to add an empty
form to each request's template context. But the posted form always
goes to a fixed view that knows how to handle the post and do your
redirect.
-RD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---