使用@login_required装饰视图时,如何匹配?next =/nextpage/值? [英] How i can match on the ?next=/nextpage/ value when i decorate a view with @login_required?

查看:37
本文介绍了使用@login_required装饰视图时,如何匹配?next =/nextpage/值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用@login_required装饰django中的视图时,如何匹配?next =/nextpage/值?它不是以标准方式"工作(通过正则表达式匹配在url.py中)!为什么它不能以标准方式工作?

How i can match on the ?next=/nextpage/ value when i decorate a view in django with @login_required? It's not working on the "standard way" (in url.py via regular expression matching)! Why it is not working on the standard way?

在urls.py中:

(r'^login/$', 'foo.nb.views.signup'),
#(r'^login/?next=(?P<next>\S{1,250})$', 'foo.nb.views.signup'),

在views.py中:

in views.py:

@login_required
def userpage(request):
    return HttpResponse('logged in -> ON USERPAGE')

再次views.py:

again views.py:

#def signup(request, next=""): # the login
 def signup(request): # the login
    """ Logs a user in """
    if request.method=="POST" and request.POST['username'] and request.POST['password']:
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)

            if user is not None:
                if user.is_active:
                    login(request, user)
                    #return redirect(next) # to sucess page
                     return redirect(request.GET['next']) # to sucess page
                else:
                    pass
                    # Return to disabled account error message
            else:
                # return invalid login error message
                pass
    return render_to_response('NBlogin.html')

当我访问views.userpage时,我将按预期重定向到我的登录页面,并且url传递了下一个字段,如/login/?next =/oldpage/,当我以正确的用户身份登录时,我收到错误消息消息告诉我,没有传递任何下一个变量(设置了用户名,设置了密码),也没有传递任何信息(但按预期的那样登录正常)

when i visit views.userpage i will be redirected to my login page as expectet, and the url have the next field passed like /login/?next=/oldpage/, when i login with a correct user i receive an error message which told me that no next variable is passed (user, password is set) as get neither as post, (but the login works fine, as expected)

Django 1.2:登录问题(GET参数:next)具有相同的主题,但是我看不到解决方案...

edit: Django 1.2: login issue (GET parameter: next) have the same topic, but i dont see a solution...

edit2:更改为request.GET ['next']后,错误消息显示:

edit2: after changing to request.GET['next'] the error message says:

Key 'next' not found in <QueryDict: {}>

edit3:

我不得不这样改变视图:

I had to change the view like so:

def signup(request): # the login
    """ Logs a user in """

    if request.method=="GET":
            try:   
                    next = request.GET['next']
            except:
                    return render_to_response('NBlogin.html')
            return render_to_response('NBlogin.html',{'next' : next})

    if request.method=="POST" and request.POST['username'] and request.POST['password']:
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)

            if user is not None:
                if user.is_active:
                    login(request, user)
                    return redirect(request.POST['next']) # to sucess page
                else:
                    pass
                    # Return to disabled account error message
            else:
                # return invalid login error message
                pass

    return render_to_response('NBlogin.html')

我不得不在模板中添加一个隐藏字段,就这样!

and i had to add a hidden field in my template, thats it!:

<form action="/login/" method="post">
        <p>Username:<input type="text" name="username"></p>
        <p>Password:<input type="password" name="password">
        <input type="hidden" name="next" value="{{next}}">
        <input type="submit" value="login"></p>
</form>

推荐答案

Django仅根据URL的路径而不是查询字符串来匹配路径.

Django only matches paths based on the path of the URL, not the querystring.

这意味着'?'之后的任何内容决定提供哪个视图时,系统不会考虑您从服务器请求中获取的请求.取而代之的是,将字符串的那部分变成像对象这样的字典.

This means that anything after '?' in your request from the server is not considered when deciding which view to serve up. Instead, that part of the string is turned into a dictionary like object.

因此,如果您要求:

/login/?next=/nextpage/

Django将使用/login/查找视图,并将使用数据 {'next':'nextpage'} 填充 request.GET 代码>.告诉我们您实际上要做什么,我将尝试为您找到一种更具Django风格的方式来实现它.

Django will use /login/ to find the view, and will populate request.GET with data {'next':'nextpage'}. Tell us what you're actually trying to do and I'll try to find a more Django-esque way for your to accomplish it.

在提供其他信息后进行

使用POST方法提交表单时,可能不会发送查询字符串?next =/nextpage/.提交表单会将数据发布到表单的action属性中指定的URL.

The querystring ?next=/nextpage/ is probably not being sent when you submit the form with the POST method. Submitting the form will POST data to the URL specified in the form's action attribute.

这篇关于使用@login_required装饰视图时,如何匹配?next =/nextpage/值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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