如何在Django Rest视图中使用login_required [英] How to use login_required in django rest view
本文介绍了如何在Django Rest视图中使用login_required的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在特定视图中使用自定义登录网址
I am trying to use a custom login url in specific view
@login_required(login_url='/account/login/')
class home(APIView):
renderer_classes = (TemplateHTMLRenderer,)
def get(self, request, format=None):
template = get_template(template_name='myapp/template.html')
return Response({}, template_name=template.template.name)
但回溯显示
File "django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)
TypeError: __init__() takes exactly 1 argument (2 given)
是否可以在基于类的视图中使用自定义登录名?
Is it possible to use custom login_required in class-based view?
谢谢!
推荐答案
我认为您正在搜索django rest框架APIView;
在这里您可以使用权限类;
请参阅此文档 http://www.django-rest-framework .org / api-guide / permissions /
I think you are searching for django rest framework APIView; Here you can use permission classes; Refer this documentation http://www.django-rest-framework.org/api-guide/permissions/
添加到seetings.py
Add to seetings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
from rest_framework.permissions import IsAuthenticated
class home(APIView):
renderer_classes = (TemplateHTMLRenderer,)
permission_classes = (IsAuthenticated,)
def get(self, request, format=None):
template = get_template(template_name='myapp/template.html')
return Response({}, template_name=template.template.name)
这篇关于如何在Django Rest视图中使用login_required的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文