简体   繁体   English

Django:登录后重定向到上一页

[英]Django: Redirect to previous page after login

I'm trying to build a simple website with login functionality very similar to the one here on SO.我正在尝试构建一个简单的网站,其登录功能与 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.我猜我必须以某种方式将当前页面的 url 传递给处理登录表单的视图,但我无法真正让它工作。

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.我通过将当前页面作为 GET 参数传递来链接到登录表单,然后使用“下一步”重定向到该页面。 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.编辑 2:我的解释似乎不清楚,所以这里要求的是我的代码:假设我们在一个页面 foo.html 上,但我们没有登录。现在我们希望在 foo.html 上有一个链接登录.html。 There we can login and are then redirected back to foo.html.在那里我们可以登录,然后被重定向回 foo.html。 The link on foo.html looks like this: foo.html 上的链接如下所示:

      <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:和 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(前4个是默认的),然后请求对象将在每个请求中可用:

settings.py:设置.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:基地.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.这将向登录页面添加一个指向当前页面的 GET 参数。

The login template can then be as simple as this:登录模板可以像这样简单:

registration/login.html:注册/登录.html:

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

To support full urls with param/values you'd need:要支持带有参数/值的完整网址,您需要:

?next={{ request.get_full_path|urlencode }}

instead of just:而不仅仅是:

?next={{ request.path }}

这可能不是“最佳实践”,但我之前已经成功使用过:

return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))

Django's built-in authentication works the way you want. Django 的内置身份验证以您想要的方式工作。

Their login pages include a next query string which is the page to return to after login.他们的登录页面包括next查询字符串,这是登录后返回的页面。

Look at http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.login_required查看http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.login_required

I encountered the same problem.我遇到了同样的问题。 This solution allows me to keep using the generic login view:此解决方案允许我继续使用通用登录视图:

urlpatterns += patterns('django.views.generic.simple',
    (r'^accounts/profile/$', 'redirect_to', {'url': 'generic_account_url'}),
)

I linked to the login form by passing the current page as a GET parameter and then used 'next' to redirect to that page.我通过将当前页面作为 GET 参数传递来链接到登录表单,然后使用“下一步”重定向到该页面。 Thanks!谢谢!

In registration/login.html (nested within templates folder) if you insert the following line, the page will render like Django's original admin login page:registration/login.html (嵌套在templates文件夹中)中,如果您插入以下行,该页面将呈现为 Django 的原始管理员登录页面:

{% include "admin/login.html" %}

Note: The file should contain above lines only.注意:该文件应仅包含以上几行。

有关views.login() ,请参阅 django 文档,您在输入表单上提供“下一个”值(作为隐藏字段)以在成功登录后重定向到。

你也可以这样做

<input type="hidden" name="text" value="{% url 'dashboard' %}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM