我的应用程序中很少有用户说 A、B 和 C。一旦任何类型的用户通过身份验证,我都不希望此用户访问我所有的 API
所以对于基于函数的视图,我实现了一个装饰器:
from functools import wraps
from rest_framework import status
from rest_framework.response import Response
def permit(user_type):
class Permission(object):
def __init__(self, view_func):
self.view_func = view_func
wraps(view_func)(self)
def __call__(self, request, *args, **kwargs):
if request.user.user_type in user_type:
return self.view_func(request, *args, **kwargs)
else:
return Response(status=status.HTTP_403_FORBIDDEN)
return Permission
Run Code Online (Sandbox Code Playgroud)
所以假设我希望我的一个 API 可以访问我所做的一种用户:
@permit(["A"])
def myview(request):
# return some reponse
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我无法将其转换为基于类的视图。
我试图装饰调度方法:
@method_decorator(permit_only(["A",]))
def dispatch(self, request, *args, **kwargs):
return super(UserList, self).dispatch(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
AssertionError(u'.accepted_renderer not set on Response',)
Run Code Online (Sandbox Code Playgroud) python decorator django-authentication django-class-based-views django-rest-framework
我有基本模板"header.html",我试图扩展它以使用django的扩展标记获取一些新数据.
了header.html
<!DOCTYPE html>
{% load staticfiles %}
<link href='https://fonts.googleapis.com/css?family=Roboto:400,500' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'font-awesome.min.css.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'bootstrap.min.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'jquery-ui.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'css/buttons.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'css/jquery.switchButton.css' %}"/>
<script src="{% static 'jquery-1.11.3.min.js' %}"></script>
<script src="{% static 'jquery-ui.js.js' %}"></script>
<script src="{% static 'jquery.dataTables.min.js' %}"></script>
<script src="{% static 'dataTables.bootstrap.min.js' %}"></script>
<script src="{% static 'common.js' …Run Code Online (Sandbox Code Playgroud)