Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

python - How to use login_required in django rest view

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)

but the traceback shows

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?

thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...