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.
10000
On Jul 2, 6:29 pm, Rajesh D <[email protected]> wrote:
> On Jul 2, 12:35 pm, 10000angrycats <[email protected]>
> wrote:
>
>
>
> > Hi everyone, long time lurker, first time botherer.
>
> > I hope someone here can help me out (& possibly a few other Django
> > n00bs in the future too).
>
> > I've dug around as best I can but can't find a - probably simple -
> > solution to an issue I'm having. I've created a simplecontactformas
> > described here:http://www.djangobook.com/en/2.0/chapter07/butI've
> > extended it slightly by using anincludeon theformto pull it in to
> > multiple templates.
>
> > & it works great for the /contact/ URL, but won't return theform
> > fields on any other views. Here's the code:
>
> > /contact/forms.py --------------
> > from django import forms
>
> > class ContactForm(forms.Form):
> > subject = forms.CharField(max_length=250)
> > email = forms.EmailField(required=False, label='Your e-mail
> > address')
> > message = forms.CharField(widget=forms.Textarea)
>
> > /contact/views.py --------------
> > from django.shortcuts import render_to_response
> > from website.contact.forms import ContactForm
> > from django.core.mail import send_mail
> > from django.http import HttpResponseRedirect
>
> > defcontact(request):
> > if request.method == 'POST':
> > form= ContactForm(request.POST)
> > ifform.is_valid():
> > cd =form.cleaned_data
> > send_mail(
> > cd['subject'],
> > cd['message'],
> > cd.get('email', '[email protected]'),
> > ['[email protected]'],
> > )
> > return HttpResponseRedirect('/contact/thanks/')
> > else:
> > form= ContactForm(
> > initial={'subject': 'Get in touch!'}
> > )
> > return render_to_response('contact/contact.html', {'form':form})
>
> > urls.py --------------
> > from django.conf.urls.defaults import *
> > from website.contact.views importcontact
> > from website.views import subdomain_homepage, qc_contact
>
> > # Enables Django Admin.
> > from django.contrib import admin
> > admin.autodiscover()
>
> > urlpatterns = patterns('',
> > (r'^admin/doc/',include('django.contrib.admindocs.urls')),
> > (r'^admin/(.*)', admin.site.root),
> > (r'^accounts/',include('registration.urls')),#Django Registration
> > URLs
> > (r'^$', subdomain_homepage),#Subdomain Homepages
> > (r'^contact/$',contact),#ContactForm
> > )
>
> > & root views.py --------------
> > from django.shortcuts import render_to_response
> > from django.contrib.sites.models import Site
> > from django.core.mail import send_mail
>
> > ...
>
> > def qc_contact(request):
> > return render_to_response('contact/contact.html', locals())
>
> > I use aninclude: {%include"contact/contact_snippet.html" %} in
> >contact.html and when I go to /contact/ it works perfectly, but
> > whenever I use theincludeon other templates theformdoesn't show.
>
> > Any ideas on the blindingly obvious that I'm missing?
>
> Just %including the contact_snippet won't work where that view doesn't
> have a "form" variable in the context. From the code you've included,
> it seems only thecontactview contains that 'form' variable. That's
> why only that view works and others don't.
>
> You could add a 'contact_form' variable to the context using a custom
> context processor. Then, every template will be able to
> see it.
>
> -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
-~----------~----~----~----~------~----~------~--~---