Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

django - URL Fragments in Redirect for LoginRequiredMixin

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...