Django(如果用户注销并且user.is_authenticated = FALSE,如何防止用户直接访问main_app / urls.py中的URL

Django的

如果用户注销并且user.is_authenticated = FALSE,如何防止用户直接访问main_app / urls.py中的URL

请注意,我在views.py中使用了基于类的视图 . 条件 if request.user.is_authenticated(): 无效 . 见下文:

class EmployeeCreate(CreateView):
    model = Employee
    fields = ['first_name', 'last_name', 'role']
    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            if "cancel" in request.POST:
                return HttpResponseRedirect(reverse('main_app:index'))
            elif "another" in request.POST:
                return HttpResponseRedirect(reverse('main_app:employee-add'))
            else:
                return super(EmployeeCreate, self).post(request, *args, **kwargs)

回答(1)