Hi Richard, Thanks for taking the tie to address my question. Here's my current views.py file. As you can see there's quite a bit of duplication. I guess what's throwing me for a loop is that I can abstract processing the form out, checking to see if the request is a post, etc, but the redirect doesn't work.
The current file: from django.shortcuts import render_to_response, get_object_or_404 from django.newforms import form_for_model from django.core.mail import send_mail from django.http import HttpResponseRedirect from lua.references.models import * from lua.references.forms import SuggestionForm reference_groups = ReferenceGroup.objects.all().order_by('order').select_related() def list_references(request): form = create_form(request) if request.method == 'POST': if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] reference_name = form.cleaned_data['reference_name'] url = form.cleaned_data['url'] subject = 'Your suggestion to Hawaiian Lua.com has been received.' message = "Thank you for suggesting %s to us. We'll review your suggestion and get back to you as quickly as possible." % (reference_name) #send_mail(subject, message, email, '[EMAIL PROTECTED]') request.session['first_name'] = first_name return HttpResponseRedirect('/articles-and-books/thanks/') return render_to_response('references.html', {'reference_groups' : reference_groups, 'form' : form}) def get_reference(request, slug): reference = get_object_or_404(Reference, slug=slug) form = create_form(request) if request.is_ajax(): template = '_content.html' elif request.method == 'POST': if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] reference_name = form.cleaned_data['reference_name'] url = form.cleaned_data['url'] subject = 'Your suggestion to Hawaiian Lua.com has been received.' message = "Thank you for suggesting %s to us. We'll review your suggestion and get back to you as quickly as possible." % (reference_name) #send_mail(subject, message, email, '[EMAIL PROTECTED]') request.session['first_name'] = first_name return HttpResponseRedirect('/articles-and-books/thanks/') else: template = 'reference_content.html' return render_to_response(template, {'reference' : reference, 'form' : form}) def thanks(request): form = create_form(request) if request.method == 'POST': if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] reference_name = form.cleaned_data['reference_name'] url = form.cleaned_data['url'] subject = 'Your suggestion to Hawaiian Lua.com has been received.' message = "Thank you for suggesting %s to us. We'll review your suggestion and get back to you as quickly as possible." % (reference_name) #send_mail(subject, message, email, '[EMAIL PROTECTED]') request.session['first_name'] = first_name return HttpResponseRedirect('/articles-and-books/thanks/') first_name = request.session['first_name'] return render_to_response('thanks.html', {'reference_groups' : reference_groups,'form' : form, 'first_name' : first_name}) def create_form(request): if request.method == 'POST': form = SuggestionForm(request.POST) else: form = SuggestionForm() return form This works, but is just hideous. I've tried doing this: def process_form(request, form): if request.method == 'POST': if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] reference_name = form.cleaned_data['reference_name'] url = form.cleaned_data['url'] subject = 'Your suggestion to Hawaiian Lua.com has been received.' message = "Thank you for suggesting %s to us. We'll review your suggestion and get back to you as quickly as possible." % (reference_name) send_mail(subject, message, email, '[EMAIL PROTECTED]') request.session['first_name'] = first_name return HttpResponseRedirect('/articles-and-books/thanks/') def list_references(request): form = create_form(request) process_form(request, form) return render_to_response('references.html', {'reference_groups' : reference_groups, 'form' : form}) But this doesn't work. The validation will fire, but the redirect will not. Thoughts? On Jun 20, 2:06 pm, "Richard Dahl" <[EMAIL PROTECTED]> wrote: > put all of the otherwise duplicative code into its own function within > another module. then import that function into your views.py, i.e: > > at the top of views.py: > from formprocessing.py import formprocessor > > then within formproccessor.py > > def processemail(email, reference_name): > subject = 'Your suggestion has been received.' > message = "Thank you for suggesting %s to us. We'll review > your suggestion and get back to you as quickly as possible." % > (reference_name) > > send_mail(subject, message, email, > '[EMAIL PROTECTED]') > > and of course in the view: > if form.is_valid(): > first_name = form.cleaned_data['first_name'] > last_name = form.cleaned_data['last_name'] > email = form.cleaned_data['email'] > reference_name = form.cleaned_data['reference_name'] > url = form.cleaned_data['url'] > > processemail(email, reference_name): > > request.session['first_name'] = first_name > return HttpResponseRedirect('/articles-and-books/thanks/') > > you could have it do all of the form processing if you need it too, just > depends on what you need. > -richard > > On 6/20/08, Brandon Taylor <[EMAIL PROTECTED]> wrote: > > > > > Hi everyone, > > > I have a feedback form which needs to be functional across 3 different > > actions within the same view... > > > My actions are: > > references_list > > reference_detail > > thanks > > > Here's my current form processing code: > > > if request.method == 'POST': > > form = SuggestionForm(request.POST) > > > if form.is_valid(): > > first_name = form.cleaned_data['first_name'] > > last_name = form.cleaned_data['last_name'] > > email = form.cleaned_data['email'] > > reference_name = form.cleaned_data['reference_name'] > > url = form.cleaned_data['url'] > > > subject = 'Your suggestion has been received.' > > message = "Thank you for suggesting %s to us. We'll review > > your suggestion and get back to you as quickly as possible." % > > (reference_name) > > > send_mail(subject, message, email, > > '[EMAIL PROTECTED]') > > request.session['first_name'] = first_name > > return HttpResponseRedirect('/articles-and-books/thanks/') > > else: > > form = SuggestionForm() > > > How can I structure this such that I don't have to repeat myself in > > each action of the view? > > > TIA, > > Brandon --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---