Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django Wrong File Path / Redirect and Pass Data

I am struggling to get this right. I am trying to redirect from PageOne to PageTwo and while supplying PageTwo with selected data from PageOne. I have tried a few things and can’t seem to get this right.

Method 1 Changing File Path

class PageOne(TemplateView):
    template_name = 'home/pageone.html'

    def get(self, request):

        args = {}
        return render(request, self.template_name, args)

    def post(self, request, *args, **kwargs):
        if request.POST.get('confirm'):
            name = request.POST.get('hidden_get_client_name')
            data= request.POST.get('get_data')

            args = {'name':name,'data':data}

            new_template_name = 'home/PageTwo.html'

            return render(request, new_template_name, args)

This works, but the file path is still /PageOne when the page loads. How do I change this to PageTwo?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Method 2 Redirect

response = redirect('/PageTwo/')
return response

Using this method, the file path is correct, but I am unable to pass data?

Any help would be greatly appreciated. I am struggling to effectively redirect between pages while passing data.

>Solution :

You can’t redirect a POST HTTP request. See more here.

You have to set the action at form to PageTwo view and to put your post handler to it as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading