Home > OS >  redirect doesn't convert special characters correctly on redirect in django
redirect doesn't convert special characters correctly on redirect in django

Time:10-07

I have a page in my django app that requires authentication. If the user hits mydomian/my_page the view will evaluate if the request is authenticated. If authenticated the request will get routed to the right html page (my_page.html). If the request is unauthenticated the request will be routed to the login page with a redirect:

class MyView(View):
    def get(self, request):
        user = request.user
        context = {
            'username': user.username
        }

        if user.is_authenticated and user.is_staff:
            return render(request,
                          'path/to/my_page.html',
                          context)
        logout(request)

        return redirect('/admin/login?next=/my_page') 

This worked pretty well, when an unauthenticated user hit mydomain/my_page it will get routed to mydomain/admin/login/?next=/my_page and once authenticated it will get routed to mydomain/my_page with will serve path/to/my_page.html.

Now I updated to the latest django version and the rerouting changed: When an unauthenticated request hits my page it gets rerouted to this url mydomain/admin/login/?next=/admin/login?next=/my_path. Once the login happens on that page, the page gets rerouted to mydomain/admin instead of mydomain/my_page. Then when I call mydomain/my_page it gets routed to the right page (since the request is authenticated).

I tried replacing the redirect with

return HttpResponseRedirect((('/admin/login?next=/my_page').encode('utf-8')))

but didn't work. I also couldn't find anything in the django docs on if and how the changed the redirect method.

Any ideas?

CodePudding user response:

Upond reviewing the docs here and here I figured the redirect now wants a slash after admin.

so this return redirect('/admin/login/?next=/my_page') solved my problem. Otherwise it's appending the provided link to my url.

  • Related