0

I'm trying to pass some parameters received from a submitted form (POST) to a ListView so that I can generate the relevant queryset based on those parameters.

I'm not clear if the parameters should be specified in the URL in addition to the View?

From current View:

        area = form.cleaned_data['area']
        service = form.cleaned_data['service']
        usage = form.cleaned_data['usage']

        return redirect('users:results', area=area, service=service, usage=usage)

urls.py

url(r'^results/$', views.ResultsView.as_view(), {}, name="results", ),

views.py

class ResultsView(ListView):
    template_name = 'site/results.html'
    paginate_by = 20
    context_object_name = 'results'

    def get_queryset(self, area, service, usage):
        results = Results.objects.filter(area=area, service=service, usage=usage)
        return results
Yunti
  • 6,761
  • 12
  • 61
  • 106
  • may be this will help. https://stackoverflow.com/questions/3024168/django-how-do-i-redirect-a-post-and-pass-on-the-post-data – badiya Jun 29 '17 at 14:38
  • Hmmm from the HTTP restriction, it looks like it's not possible to use a redirect with POST data. Is there a better method instead? - call the view function directly and pass the parameters in (as this looks like a fairly common problem - ie accept data from form to generate a specific list of results to display based on the form data) – Yunti Jun 29 '17 at 14:46
  • 1
    Why does it need to be a post, and why do you need to redirect? Why not use a GET and just show the response directly? – Daniel Roseman Jun 29 '17 at 14:58
  • Not sure what you mean by show the response directly? Do you mean in the same view? – Yunti Jun 29 '17 at 15:59
  • I'm using a POST request here as some of the user data is slightly sensitive(this is a shortened example) and I would rather not expose it in the URL. – Yunti Jun 29 '17 at 17:06
  • My be you can try storing the data in `request.session` and then read that in you `Listview's` `get` method. – badiya Jun 30 '17 at 06:13
  • Thanks I have stored it in a session for now, but that requires it to be stateful and it feels like there should be a better solution than saving the data to the session only to be used in the next view. – Yunti Jul 04 '17 at 13:25

0 Answers0