1

I have a requirement to display a particular page in a website that is developed in Python/Django from another website. If the user is logged into the python/Django site then display the requested page. Currently i'm using the 'request.user.is_authenticated()' method to check whether the user is logged in or not . If the user hasn't logged in then should redirect to the /login page and once the user has entered his login credentials correctly redirect to the particular requested page.

The problem is if the user is not logged in then should redirect to login page. In this case the URL gets changed . We need to redirect back to the previous URL once the user is logged in. How can i achieve this? The URL 'm calling is not a static URL. The parameters in the URL changes. I'm searching for some built in methods in Django to achieve this..

Does some have a better idea to solve this.

Thanks in advance.

4

1 回答 1

2

The default implementation of login_required accepts a next parameter which will cause the user to redirect to that URL after successful login.

https://docs.djangoproject.com/en/dev/topics/auth/default/#the-login-required-decorator

But of course you can recreate this functionality with a few lines.. in your login view, check for the presence of a get parameter called next or redirect_url, then redirect to it upon successful login.

The view sending the user to the login page should simply append `'?next=%s' % request.path

Simple as that.

于 2013-01-29T06:13:44.757 回答