- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在编写一些 View 来响应 ajax 请求时,我发现 login_required 装饰器总是为未通过身份验证的用户返回 302 状态代码,这有点奇怪。由于这些 View 是 ajax View ,这似乎有些不合适。我不希望用户在这种情况下登录,但我希望 Django 告诉客户端访问这样的 View 需要身份验证(我认为 401 应该是正确的状态代码)。
为了实现这一点,我开始编写自己的装饰器 login_required_ajax,但不知何故这超出了我的技能。这是我到目前为止想出的:
def login_required_ajax(function=None,redirect_field_name=None):
"""
Just make sure the user is authenticated to access a certain ajax view
Otherwise return a HttpResponse 401 - authentication required
instead of the 302 redirect of the original Django decorator
"""
def _decorator(view_func):
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated():
return view_func(request, *args, **kwargs)
else:
return HttpResponse(status=401)
if function is None:
return _decorator
else:
return _decorator(function)
最佳答案
这是一个很好的尝试。这是我发现的几个问题:
_decorator
函数应该返回 _wrapped_view
. if function is None
块有点偏离-login_required_ajax
函数需要返回被修饰的函数。 def login_required_ajax(function=None,redirect_field_name=None):
"""
Just make sure the user is authenticated to access a certain ajax view
Otherwise return a HttpResponse 401 - authentication required
instead of the 302 redirect of the original Django decorator
"""
def _decorator(view_func):
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated():
return view_func(request, *args, **kwargs)
else:
return HttpResponse(status=401)
return _wrapped_view
if function is None:
return _decorator
else:
return _decorator(function)
关于ajax - ajax View 上的 login_required 装饰器返回 401 而不是 302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031001/
我是django的新手,我很困惑Django中的@login_required和@method_decorator(login_required)有什么区别,我们应该使用哪个。提前致谢。 最佳答案 在
我正在为我的应用程序使用flask-login插件,该插件已经得到了很大程度的开发。 我想将所有URL端点标记为“ @login_required”。 Is there any way I can d
我正在使用 Django 消息框架向用户显示消息以及 @login_required装饰师对我的看法之一。因此,如果用户试图在未登录的情况下访问某个 View ,他们将被踢到登录页面。我将如何向登录页
我在 Django 中使用基于类的 View 。 @login_required 装饰器没有重定向到登录页面。它仍然显示个人资料页面。 class ProfileView(TemplateView):
@login_required 装饰器似乎对我没有任何作用。如果用户未登录并调用注册,我希望它重定向到登录页面。我错过了什么吗?我什至尝试设置 @login_required(login_url='/
我在 Django 中使用基于类的 View 。 @login_required 装饰器没有重定向到登录页面。它仍然显示个人资料页面。 class ProfileView(TemplateView):
@login_required 装饰器似乎对我没有任何作用。如果用户未登录并调用注册,我希望它重定向到登录页面。我错过了什么吗?我什至尝试设置 @login_required(login_url='/
你好,我有以下看法: class BookList(RequireLoginMixin, generic.ListView): model = Book template_name =
我正在使用 i18n_patterns 来国际化我的应用程序并且它正在运行,除非我单击需要登录的链接(受 @login_required 装饰器保护的 View ),我是被重定向到默认语言的登录表单,
根据 https://stackoverflow.com/a/8715790/210481 中的评论之一,我同意,如果一个依赖于另一个,我们应该避免使用多个装饰器。 因此,在示例中,如果我们有一个用于
我正在尝试在具有 login_required 的 View 上测试发布请求。 如果我在 shell django 中运行它: Client().post('/recipes/', {'name' :
我正在为我的应用程序中的某些路由使用 Flask-login 的 @login_required 装饰器。在未登录的情况下导航到这些路线时,我会被重定向到登录页面。到目前为止一切顺利。 重定向的网址如
我正在运行 django 1.1rc。使用 Django 的内置开发服务器,我的所有代码都能正常工作;但是,当我使用 Apache 的 mod_python 将其投入生产时,我在所有 View 中都收
我正在尝试限制 View ,除非完成登录。正在关注 tuts+plus django unchained 教程 但是 login_required 装饰器不知何故没有完成这项工作。我做错了什么? vi
我正在将flask-login 与postgreSQL 结合使用。每当我在任何 def 之前有一个装饰器 @login_required 时,它不会重定向到该特定的 def。这是我的代码: init.
我想将 login_required 与基于函数的 View 一起使用。我浏览了 Django's official docs of django.contrib.auth.decorators.lo
我正在尝试使用 SSL 在本地测试我的 Django 应用程序。我有一个带有 @login_required 装饰器的 View 。因此,当我点击 /locker 时,我会被重定向到 /locker/
默认情况下,当使用 @login_required 装饰器时,Django 会在将未经身份验证的用户重定向到登录页面时执行 302(临时)重定向。我与一家 SEO 公司合作(我自己对这个话题一无所知)
我在我的应用程序中使用 Flask-HTTPAuth 来处理身份验证。我有很多观点,我不想给每一个都加上login_required。如何让默认要求登录? from flask.ext.httpaut
我已经创建了一个处理身份验证的蓝图。此蓝图使用 Flask-Login。并具有以下内容以及未显示的更多代码。 在蓝图中我有以下内容: from flask.ext.login import Login
我是一名优秀的程序员,十分优秀!