Cool. I'll leave it be an more on :) Thanks again for your time and patience! Brandon
On Jun 20, 3:38 pm, "Richard Dahl" <[EMAIL PROTECTED]> wrote: > Its possible of course to collapse some of the views together, but that > would result in more complex logic in the views and probably not worth the > trouble, I probably would not do it myself in this situation, although I > have done simliar things with larger projects. It comes down to where do > you want the complexity to live, the templates (including javascript > functions), the urls configuration, or the views (and sometimes within the > models). I have a tendency to write pretty complex views (and model > functions) for processing asynchronous stuff to limit the complexity of my > javascript, but that is primarily because I am far more comfortable with > python than JS. > -richard > > On 6/20/08, Brandon Taylor <[EMAIL PROTECTED]> wrote: > > > > > Ah, I see now. > > > Here's my views.py file now: > > > 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': > > process_form(request, form) > > 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': > > process_form(request, form) > > 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': > > process_form(request, form) > > 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 > > > def process_form(request, form): > > 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 > > > Do you see anything else that can be encapsulated along the same line? > > I don't have as much redundancy now, but if this is as short as it can > > get, it's as short as it can get. Thank you SO much for your help. I'm > > still getting used to Python, but it's very, very good. > > > Kind regards, > > Brandon > > > On Jun 20, 3:02 pm, "Richard Dahl" <[EMAIL PROTECTED]> wrote: > > > OK I *think* I see what is wrong: > > > You are returning the redirect from within the 'process_form' function, > > but > > > not assigning it to anything or further processing it. SO, the process > > form > > > returns a redirect, which is promptly and efficiently ignored, then > > returns > > > a render_to_response from the view. > > > > Move: > > > return HttpResponseRedirect('/articles-and-books/thanks/') > > > back to the views and get rid of the render_to_response. > > > > On 6/20/08, Brandon Taylor <[EMAIL PROTECTED]> wrote: > > > > > 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 > > ... > > read more » --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---