我有一些难以理解新的类视图(Class Based Views)是如何工作的。我的问题是,我需要在所有视图中要求登录,在某些视图中还需要特定权限。在基于函数的视图中,我使用@permission_required()和视图中的login_required属性来完成这个操作,但是我不知道如何在新的视图上实现这个功能。Django文档中有关于这方面的介绍吗?我没有找到任何相关内容。我的代码哪里出了问题呢?
我尝试使用@method_decorator,但它回复错误:TypeError at /spaces/prueba/ _wrapped_view() takes at least 1 argument (0 given) 以下是代码(GPL):
我尝试使用@method_decorator,但它回复错误:TypeError at /spaces/prueba/ _wrapped_view() takes at least 1 argument (0 given) 以下是代码(GPL):
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required, permission_required
class ViewSpaceIndex(DetailView):
"""
Show the index page of a space. Get various extra contexts to get the
information for that space.
The get_object method searches in the user 'spaces' field if the current
space is allowed, if not, he is redirected to a 'nor allowed' page.
"""
context_object_name = 'get_place'
template_name = 'spaces/space_index.html'
@method_decorator(login_required)
def get_object(self):
space_name = self.kwargs['space_name']
for i in self.request.user.profile.spaces.all():
if i.url == space_name:
return get_object_or_404(Space, url = space_name)
self.template_name = 'not_allowed.html'
return get_object_or_404(Space, url = space_name)
# Get extra context data
def get_context_data(self, **kwargs):
context = super(ViewSpaceIndex, self).get_context_data(**kwargs)
place = get_object_or_404(Space, url=self.kwargs['space_name'])
context['entities'] = Entity.objects.filter(space=place.id)
context['documents'] = Document.objects.filter(space=place.id)
context['proposals'] = Proposal.objects.filter(space=place.id).order_by('-pub_date')
context['publication'] = Post.objects.filter(post_space=place.id).order_by('-post_pubdate')
return context