Home > Enterprise >  Django Wrong File Path / Redirect and Pass Data
Django Wrong File Path / Redirect and Pass Data

Time:12-25

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?

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.

CodePudding user response:

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.

  • Related