Django的1.2:登录问题(GET参数:下一个) [英] Django 1.2: login issue (GET parameter: next)

查看:201
本文介绍了Django的1.2:登录问题(GET参数:下一个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Django的一个新的问题(我张贴失去了他们这些天^^)。

下面是我的情况:我有一个自定义登录视图(注册为设置登录网址),我验证用户。我选择了做一个自定义视图可以添加信息和记录。

认证工作很好,但我有'下一个'把GET参数有问题。它是通过重定向进行身份验证的用户的意见自动设置。这是在我看来,使用成功登录后,将用户重定向。

下面是code:

 从django.http进口的Htt presponse
从django.core.urlresolvers进口反
从django.http进口的Htt presponseRedirect
从django.shortcuts进口render_to_response
从django.utils.translation进口ugettext为_
从进口django.contrib中的消息
从django.template进口的RequestContext
从django.contrib.auth进口身份验证,登录,注销进口记录
记录= logging.getLogger(意见)高清login_user(要求):
    
    显示登录表单或认证,如果通过所谓的POST用户。
    
    接受=假    接下来= request.GET.get(下一步,无)    如果不是request.user.is_authenticated():
        如果request.POST:
            #获取表单数据,并检查用户凭据
            用户名= request.POST ['用户名']
            密码= request.POST ['密码']
            用户身份验证=(用户名=用户名,密码=密码)            如果用户不无:
                如果user.is_active:
                    #登录用户在
                    登录(请求用户)
                    logger.info(中登录的用户:%S,user.username)                    #确认登录
                    messages.success(要求,_('登录成功。欢迎您!'))
                    接受= TRUE
                其他:
                    messages.error(要求,_('此帐户已被管理员禁用。'))
            其他:
                messages.warning(要求,_('给定的用户名或密码无效,请重试。'))
    其他:
        #如果已通过身份验证
        接受= TRUE    #选择去哪里
    如果接受的话:
        返回的Htt presponse(下)
        如果下一个:
            返回的Htt presponseRedirect(下)
        其他:
            返回的Htt presponseRedirect(逆转(MyView的'))
    其他:
        返回render_to_response('的login.html',
                                    context_instance = RequestContext的(要求))

当用户访问时,已认证(第一个else)登录视图下一个参数是正确的。

当匿名用户尝试访问/编辑/ 25(例如),并重定向到登录进行身份验证,下一个永远是无即使在URL present(应该是/编辑/ 25)。

应该有一个简单的错误的地方。也许它与身份验证()或登录()(django.contrib.auth)做。

感谢您的帮助。


解决方案

  

当匿名用户尝试访问/编辑/ 25(例如),并重定向到登录进行身份验证,下一个永远是无即使在URL present(应该是/编辑/ 25)。


这听起来很奇怪。你可以检查网址确实有?下次= /编辑/ 25 / 作为查询字符串?同时登录 request.GET中,看看轮番上涨。

此外,您可能想拿起从 request.GET中接下来参数,并把它作为一个(可选)的形式隐藏输入渲染模板时。在auth模块的登录视图做到这一点。这样,你的表格可以在 POST 接下来 request.POST $ C>。

I have a new question about django (I post a lost of them these days^^).

Here is my situation : I have a custom login view (registered as login url in the settings) where I authenticate the users. I chose to do a custom view to be able to add messages and logging.

The authentication works well, but I have a problem with the GET parameter 'next'. It is set automatically by the views redirecting the users for authentication. It is used in my view to redirect the user after a successful login.

Here is the code :

from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.template import RequestContext
from django.contrib.auth import authenticate, login, logout

import logging
logger = logging.getLogger("views")

def login_user(request):
    """
    Displays the login form or authenticates the user if called by POST.
    """
    accepted = False

    next = request.GET.get('next', None)

    if not request.user.is_authenticated():
        if request.POST:
            # Get the form data and check the user credentials
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)

            if user is not None:
                if user.is_active:
                    # Log the user in
                    login(request, user)
                    logger.info("Login of user : %s", user.username)

                    # Confirm the login
                    messages.success(request,_('Login successful. Welcome !'))
                    accepted = True
                else:
                    messages.error(request,_('This account has been disabled by the administrator.'))
            else:
                messages.warning(request,_('The given username or password is invalid. Please try again.'))
    else:
        # If already authenticated
        accepted = True

    # Choose where to go
    if accepted:
        return HttpResponse(next)
        if next:
            return HttpResponseRedirect(next)
        else:
            return HttpResponseRedirect(reverse('myview'))
    else:
        return render_to_response('login.html',
                                    context_instance=RequestContext(request))

The next parameter is correct when the user is accessing the login view when already authenticated (first else).

When an anonymous user tries to go to /editor/25 (for example) and is redirected to login for authentication, "next" is always None even if present in the url (it should be "/editor/25").

There should be one simple error somewhere. Maybe it has to do with authenticate() or login() (django.contrib.auth).

Thanks for your help.

解决方案

When an anonymous user tries to go to /editor/25 (for example) and is redirected to login for authentication, "next" is always None even if present in the url (it should be "/editor/25").

This sounds strange. Can you check that the URL indeed has ?next=/editor/25/ as the query string? Also log request.GET and see what turns up.

Also, you may want to pick up the next parameter from request.GET and include it as a (optional) hidden input in the form when rendering the template. The auth module's login view does this. This way your form can pick up next from request.POST on POST.

这篇关于Django的1.2:登录问题(GET参数:下一个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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