4

Edit:
I want the 'success_url' (ie, result.html) to display the 'data' from 'form.process()'. The following code obviously doesn't work. Can anyone please tell me what's wrong with it or suggest another way to basically view the context 'data' in a template (either in the form of list or dict), ie a better way to display data to the user after a form has been submitted.
Many thanks in advance.

-- urls.py --
url(r'^$', view='main_view'),
url(r'^result/$', view='result_view'),

-- views.py --
class ResultView(TemplateView):
    template_name = "result.html"

class MainView(FormView):
    template_name = 'index.html'
    form_class = UserInputForm
    success_url = 'result/'

    def form_valid(self, form):
        data = form.process()
        return super(MainView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(MainView, self).get_context_data(**kwargs)
        context['data'] = data
        return context

main_view = MainView.as_view()
result_view = ResultView.as_view()
DGT
  • 2,604
  • 13
  • 41
  • 60

4 Answers4

10

As far as I understood your question, you want to show the contents of the user submitted form in the result view. Is that correct?

In this case the method get_context_data won't help you at all, because it will only store data in the current context which is in MainView.

The form_valid method of FormView will make a HttpResponseRedirect to the success_url. So the question now is, how can we give the data to this view.

As explained in Django return redirect() with parameters the easiest way would be to put the data into the session. In the result.html-template you could then access this data as explained in Django: accessing session variables from within a template?

Here is the code:

class ResultView(TemplateView):
    template_name = "result.html"

class MainView(FormView):
    template_name = 'index.html'
    form_class = UserInputForm
    success_url = 'result/'

    def form_valid(self, form):
        self.request.session['temp_data'] = form.cleaned_data
        return super(MainView, self).form_valid(form)

in the result.html template you could then access this temp_data so:

{{ request.session.temp_data }}
Community
  • 1
  • 1
DanEEStar
  • 6,140
  • 6
  • 37
  • 52
  • Yes! 'session' is what I was looking for. Gr8. Thanks DanEEStar. – DGT Oct 10 '12 at 19:49
  • To add: you'll need `django.core.context_processors.request` in your `context_processors` for this to work. – SaeX Oct 24 '15 at 11:20
1

As suggested above, you can override get_context_data.

For example, you can do something like the below:

def get_context_data(self, **kwargs):
    context = super(MainView, self).get_context_data(**kwargs)
    #set some more context below.
    context['foo'] = bar
    ...
    return context
  • Thanks, kranthi, but overriding the 'get_context_data' doesn't get me 'foo' in the 'result.html' template, which is what I'm trying to achieve, unless someone knows a better way to display the data to the user after a form is submitted. – DGT Oct 09 '12 at 14:40
0

Look for get_context_data in context the Django class-based view docs. The dict returned by the overridden method will be passed into the templates.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
0

There are a couple of things that could be your problem. First, in form_valid() method, you process the form before you call that class' parent form_valid(). Also, you're no storing the result in a common place for both methods to grab it. Try something like:

def form_valid(self, form):
    self.data = form.cleaned_data
    return super(MainView, self).form_valid(form)

def get_context_data(self, **kwargs):
    context = super(MainView, self).get_context_data(**kwargs)
    context['data'] = self.data
    return context
pyriku
  • 1,251
  • 7
  • 17
  • Thanks, but sorry that doesn't seem to work. I get "'MainView' object has no attribute 'data'" error. Besides, form.process() doesn't seem to be kicking in. I get nothing if I try print in it. – DGT Oct 09 '12 at 14:22
  • What is process()? What are you trying to do? Maybe you can just store the result of form_valid in self.data and use it in get_context_data. – pyriku Oct 09 '12 at 15:25
  • I'm new to django, so please bear with me. The 'UserInputForm' is a form which when submitted by the user, I want the user input/selected form values (from textarea, dropdown, etc.) to pass through a method in a class, and the returned values to be displayed on a template ('result.html'). The fore mentioned 'process' refers to that calling of another method. – DGT Oct 09 '12 at 16:59
  • So, if I understand it right, what you want is to show in "result.html" what the user introduced into the form? If so, check the code as I just modified. form.cleaned_data has a dictionary with the cleaned values for each field. – pyriku Oct 10 '12 at 18:26