Django的认证和阿贾克斯 - 网址需要登录 [英] Django authentication and Ajax - URLs that require login

查看:174
本文介绍了Django的认证和阿贾克斯 - 网址需要登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些阿贾克斯 -niceness添加到我的Django- codeD网站。

I want to add some Ajax-niceness to my Django-coded website.

在我的Django code,我使用 django.contrib.auth.decorators的 @login_required 装饰标记某些观点需要验证。当未通过身份验证用户点击它重定向他的默认行为/她到登录页面,然后通过目标页面。

In my Django code, I use the @login_required decorator from django.contrib.auth.decorators to mark which view requires authentication. The default behavior when a not authenticated user clicks it is to redirect him/her to login page, and then pass the target page.

我看到一些网站上,和真的很喜欢,就是当用户点击一个链接,导致限制为登录用户只能一个地方,而不是重新导向到登录页面,他/她得到一个弹出窗口(通过JavaScript)的要求他/她登陆或注册。有没有重定向的一部分,因此没有必要为用户使用后退键,如果他/她决定他/她真的不喜欢的网站够浪费时间注册。

What I saw on some sites, and really liked, is that when user clicks a link leading to a place restricted to logged-only users, instead of getting redirected to a login page, he/she gets a popup window (via JavaScript) asking him/her to log in or register. There's no redirection part, so no need for a user to use the "back" key if he/she decides he/she really doesn't like the website enough to waste the time registering.

所以,qestion是:你将如何管理为限制,因此JavaScript可以处理他们的的onclick 事件,显示自动打一些链接任务,请登录在弹出?

So, the qestion is: how would you manage the task of automatically marking some links as "restricted" so JavaScript can handle their onclick event and display a "please log in" popup?

推荐答案

我面临着同样的问题,你一样,我想一个简单的装饰包裹,以便在同一个来处理身份验证围绕一个Django ajax的观点的方式,我有其他的看法。一种方法,似乎很有希望我是结合使用这样的装饰用JavaScript,看起来在响应一定的价值。

I am facing the same issue, and, like you, I would like a simple decorator to wrap around a Django ajax view in order to handle authentication in the same way that I have other views. One approach that seems promising to me is to use such a decorator in conjunction with JavaScript that looks for a certain value in the response.

下面是第一个装饰的修订草案:

Here is first revised draft of the decorator:

from functools import wraps

def ajax_login_required(view_func):
    @wraps(view_func)
    def wrapper(request, *args, **kwargs):
        if request.user.is_authenticated():
            return view_func(request, *args, **kwargs)
        json = simplejson.dumps({ 'not_authenticated': True })
        return HttpResponse(json, mimetype='application/json')
    return wrapper

下面认为:

@ajax_login_required
def ajax_update_module(request, module_slug, action):
    # Etc ...
    return HttpResponse(json, mimetype='application/json')

这是JavaScript(jQuery的):

And here is the JavaScript (jQuery):

$.post('/restricted-url/', data, function(json) {
    if (json.not_authenticated) {
        alert('Not authorized.');  // Or something in a message DIV
        return;
    }
    // Etc ...
});


修改:我试图使用 functools.wraps 的建议。在工作​​code我还没有实际使用这个装饰,所以可能的错误的要小心了。


EDIT: I've attempted to use functools.wraps, as suggested. I have not actually used this decorator in working code, so beware of possible bugs.

这篇关于Django的认证和阿贾克斯 - 网址需要登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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