Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Fragments in Redirect for LoginRequiredMixin

Tags:

django

I am using django-braces's LoginRequiredMixin in a Django 1.6 project. This mixin replicates Django's login_required decorator.

I have a view that uses the LoginRequiredMixin that has a URL like this: /spa_home/#price_requests/68. If I try to hit this URL without being logged in, the mixin correctly sends me to the login page with a request like this: /accounts/login/?next=/spa_home/#price_requests/68. Unfortunately, after successfully logging in, the URL hash fragment is left off and I am just redirected to /spa_home/.

What is the best way to fix this? Removing hash fragments from my application would be a large effort.

like image 957
Erik Avatar asked Feb 02 '26 22:02

Erik


1 Answers

The issue is the way the browser interprets the login URL. You want it to be intepreted like this:

/accounts/login/?next="/spa_home/#price_requests/68"

but actually, it is seen like this:

"/accounts/login/?next=/spa_home/"#price_requests/68

In other words, the hash is seen as attaching to the login URL itself, not the redirect parameter.

The way to fix this is to quote the parameter:

urllib.quote('/spa_home/#price_requests/68')

which gives you /spa_home/%23price_requests/68, which will be interpreted correctly.

like image 184
Daniel Roseman Avatar answered Feb 04 '26 15:02

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!