3

Using a generic FormView I'd like to reflect something about the POST data that was submitted back to the user, but I'm not sure how best to do this.

class MyView(generic.FormView):
    form_class = MyForm
    def get_success_url(self):
        return reverse('success')

reverse('success') redirects to a simple

class SuccessView(generic.TemplateView):
    template_name = 'success.html'

Is there a way that I can access params object in SuccessView via the get_success_url call, or is there a better (and simpler) way to do this? TIA Dan

UPDATE (my solution, but thanks for the ideas)

I actually found that this was the simplest way (for me) to solve the problem:

class SuccessMixin(object):
    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)

        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form, **kwargs)

    def form_valid(self, form):
        # this is my post processing step which returns the
        # feedback data that I want to pass to the user
        form_data = form.get_some_data_from_form()
        return render_to_response('submitted.html', 
            {'form_data': form_data,}, 
            context_instance=RequestContext(self.request))

Each view inherits this mixin, and if the form is valid then I pull the feedback data from it and render it as a response - completely bypassing the get_success_url redirect. I've removed the get_success_url and SuccessView.

danodonovan
  • 19,636
  • 10
  • 70
  • 78

3 Answers3

5

Include the data as GET parameters in your success url.

def get_success_url(self):
   url = reverse('success')
   # maybe use urlencode for more complicated parameters
   return "%s?foo=%s" % (url, self.request.POST['bar'])

Then in your template, access the parameters in the GET data.

foo: {{ request.GET.foo }}
Alasdair
  • 298,606
  • 55
  • 578
  • 516
2

I was trying to figure this out too and a simple solution is to use:

from django.contrib import messages

In get_success_url(self) you can write

def get_success_url(self)
    messages.add_message(self.request, messages.SUCCESS, 'Contact Successfully Updated!')
    return reverse('contacts:contact_detail', args=(self.get_object().id,))

And in your template you can access this message via

{% for message in messages %}
    {{ message }}
{% endfor %}
Lehrian
  • 347
  • 1
  • 2
  • 9
1

One way to accomplish what you're looking to do would be through the use of Sessions. Store whatever values you need on the user's session object, and access them from within the SuccessView. Check out the Django session documentation for more details.

fourk
  • 2,224
  • 1
  • 16
  • 10
  • Hmm - all the forms are unique and so using Sessions would obliterate the DRY (loading each field from any form into the session). Thanks for the idea though. – danodonovan Feb 13 '12 at 22:42
  • Why not make a parent form that all of your forms inherit from. Override the __init__ or the is_valid method on it (hard to say which without any examples of how you're using the forms in views, how the views differ, how the forms differ, etc), load the relevant parts of the request into the users session from there? I don't understand where any code would be duplicated using that approach. – fourk Feb 13 '12 at 22:52