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 simple contact form as
> described here:http://www.djangobook.com/en/2.0/chapter07/but I've
> extended it slightly by using an include on the form to pull it in to
> multiple templates.
>
> & it works great for the /contact/ URL, but won't return the form
> 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
>
> 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', '[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 import contact
> 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),#Contact Form
> )
>
> & 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 an include: {% include "contact/contact_snippet.html" %} in
> contact.html and when I go to /contact/ it works perfectly, but
> whenever I use the include on other templates the form doesn'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 the contact view 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
-~----------~----~----~----~------~----~------~--~---