diff options
author | luxagraf <sng@luxagraf.net> | 2012-09-22 22:27:04 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2012-09-22 22:27:04 -0400 |
commit | efb623af0bcb47d510501c282e1326b11343a29c (patch) | |
tree | 3a35fb19f5eba3b219c65277a5fb712cbe9604ac /app/lib/contact_form/views.py | |
parent | 0b481fd7931c2ae20ca21f89a87f2ba6a6c01e10 (diff) |
site reorg
Diffstat (limited to 'app/lib/contact_form/views.py')
-rw-r--r-- | app/lib/contact_form/views.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/app/lib/contact_form/views.py b/app/lib/contact_form/views.py new file mode 100644 index 0000000..fc33f4a --- /dev/null +++ b/app/lib/contact_form/views.py @@ -0,0 +1,66 @@ +""" +View which can render and send email from a contact form. + +""" + + +from django.http import HttpResponseRedirect +from django.shortcuts import render_to_response +from django.template import RequestContext +from django.contrib.auth.views import redirect_to_login + +from contact_form.forms import ContactForm + + +def contact_form(request, form_class=ContactForm, + template_name='contact_form/contact_form.html', + success_url='/contact/sent/', login_required=False, + fail_silently=False): + """ + Renders a contact form, validates its input and sends an email + from it. + + To specify the form class to use, pass the ``form_class`` keyword + argument; if no ``form_class`` is specified, the base + ``ContactForm`` class will be used. + + To specify the template to use for rendering the form (*not* the + template used to render the email message sent from the form, + which is handled by the form class), pass the ``template_name`` + keyword argument; if not supplied, this will default to + ``contact_form/contact_form.html``. + + To specify a URL to redirect to after a successfully-sent message, + pass the ``success_url`` keyword argument; if not supplied, this + will default to ``/contact/sent/``. + + To allow only registered users to use the form, pass a ``True`` + value for the ``login_required`` keyword argument. + + To suppress exceptions raised during sending of the email, pass a + ``True`` value for the ``fail_silently`` keyword argument. This is + **not** recommended. + + Template:: + + Passed in the ``template_name`` argument. + + Context:: + + form + The form instance. + + """ + if login_required and not request.user.is_authenticated(): + return redirect_to_login(request.path) + + if request.method == 'POST': + form = form_class(data=request.POST, request=request) + if form.is_valid(): + form.save(fail_silently=fail_silently) + return HttpResponseRedirect(success_url) + else: + form = form_class(request=request) + return render_to_response(template_name, + { 'form': form }, + context_instance=RequestContext(request)) |