24๐
I think you are searching for Django Rest Framework APIView.
Here you can use permission classes: http://www.django-rest-framework.org/api-guide/permissions/
On your projectโs seetings.py
:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
And on your code:
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)
13๐
Decorators can only be used on functions, not classes.
However, for decorating class-based views the django docs suggest this:
Decorating the class
To decorate every instance of a class-based view, you need to decorate
the class definition itself. To do this you apply the decorator to the
dispatch()
method of the class.A method on a class isnโt quite the same as a standalone function, so
you canโt just apply a function decorator to the method โ you need to
transform it into a method decorator first. Themethod_decorator
decorator transforms a function decorator into a method decorator so
that it can be used on an instance method. For example:from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.views.generic import TemplateView class ProtectedView(TemplateView): template_name = 'secret.html' @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(ProtectedView, self).dispatch(*args, **kwargs)
- [Django]-Django โ How to track if a user is online/offline in realtime?
- [Django]-Django 1.9 โ JSONField in Models
- [Django]-Do we need to upload virtual env on github too?
2๐
Since Django 1.9 you can alternatively use a Mixin for controlling permissions in class based views:
https://docs.djangoproject.com/en/1.9/releases/1.9/#permission-mixins-for-class-based-views
https://docs.djangoproject.com/en/1.9/topics/auth/default/#django.contrib.auth.mixins.LoginRequiredMixin
- [Django]-Change a form value before validation in Django form
- [Django]-Django โ after login, redirect user to his custom page โ> mysite.com/username
- [Django]-Django Heroku Error "Your models have changes that are not yet reflected in a migration"