Django:登录后重定向到上一页 [英] Django: Redirect to previous page after login

查看:116
本文介绍了Django:登录后重定向到上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个简单的网站,其登录功能非常类似于SO上的一个。
用户应该能够以匿名用户身份浏览网站,并且每个页面上都会有一个登录链接。当点击登录链接时,用户将被带到登录表单。成功登录后,用户应该被重新回到他点击登录链接的页面。
我猜想,我必须以某种方式将当前页面的URL传递给处理登录表单的视图,但我无法真正使其正常工作。



&p编辑:
我想出来了我通过将当前页面作为GET参数传递到登录表单,然后使用next重定向到该页面。谢谢!



编辑2:
我的解释似乎不清楚,所以这里要求的是我的代码:
说我们在页面foo.html,我们还没有登录。现在我们想在foo.html上链接到login.html。在那里我们可以登录,然后重定向回foo.html。
foo.html上的链接如下:

 < a href ='/ login /?next = {{request.path}}'>登录< / a> 

现在我写了一个自定义登录视图,看起来像这样:

  def login_view(request):
redirect_to = request.REQUEST.get('next','')
if request.method = ='POST':
#create login form ...
如果输入了有效的登录凭据:
return HttpResponseRedirect(redirect_to)
#...
return render_to_response('login.html',locals())

而login.html中的重要行:

 < form method =postaction =./?next = {{redirect_to}}> 

所以,这几乎是这样,希望能够清楚。

解决方案

您不需要为此添加额外的视图,功能已经内置。



首先使用登录链接的每个页面需要知道当前的路径,最简单的方法是将请求上下文预处理程序添加到settings.py(第一个是默认值),然后在每个请求中都可以使用该请求对象: / p>

settings.py:

  TEMPLATE_CONTEXT_PROCESSORS =(
django.core.context_processors.auth,
django.core.context_processors.debug,
django.core.context_processors.i18n,
django .core.context_processors.media
django.core.context_processors.request,

然后添加您想要登录链接的模板:



base.html:

 <啊ref ={%url django.contrib.auth.views.login%}?next = {{request.path}}>登录< / a> 

这将向登录页面添加一个GET参数,指向当前页面。 >

登录模板可以这样简单:



registration / login.html:

  {%block content%} 
< form method =postaction =>
{{form.as_p}}
< input type =submitvalue =Login>
< / form>
{%endblock%}


I'm trying to build a simple website with login functionality very similar to the one here on SO. The user should be able to browse the site as an anonymous user and there will be a login link on every page. When clicking on the login link the user will be taken to the login form. After a successful login the user should be taken back to the page from where he clicked the login link in the first place. I'm guessing that I have to somehow pass the url of the current page to the view that handles the login form but I can't really get it to work.

EDIT: I figured it out. I linked to the login form by passing the current page as a GET parameter and then used 'next' to redirect to that page. Thanks!

EDIT 2: My explanation did not seem to be clear so as requested here is my code: Lets say we are on a page foo.html and we are not logged in. Now we would like to have a link on foo.html that links to login.html. There we can login and are then redirected back to foo.html. The link on foo.html looks like this:

      <a href='/login/?next={{ request.path }}'>Login</a> 

Now I wrote a custom login view that looks somewhat like this:

def login_view(request):
   redirect_to = request.REQUEST.get('next', '')
   if request.method=='POST':
      #create login form...
      if valid login credentials have been entered:
         return HttpResponseRedirect(redirect_to)  
   #...
   return render_to_response('login.html', locals())

And the important line in login.html:

<form method="post" action="./?next={{ redirect_to }}">

So yeah thats pretty much it, hope that makes it clear.

解决方案

You do not need to make an extra view for this, the functionality is already built in.

First each page with a login link needs to know the current path, and the easiest way is to add the request context preprosessor to settings.py (the 4 first are default), then the request object will be available in each request:

settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
)

Then add in the template you want the Login link:

base.html:

<a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a>

This will add a GET argument to the login page that points back to the current page.

The login template can then be as simple as this:

registration/login.html:

{% block content %}
<form method="post" action="">
  {{form.as_p}}
<input type="submit" value="Login">
</form>
{% endblock %}

这篇关于Django:登录后重定向到上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆