Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django(如果用户已注销且User.is_authenticated=FALSE,如何防止用户直接访问main_app/url.py中的URL_Django_Django Views_Django Authentication - Fatal编程技术网

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

Django(如果用户已注销且User.is_authenticated=FALSE,如何防止用户直接访问main_app/url.py中的URL,django,django-views,django-authentication,Django,Django Views,Django Authentication,德扬戈 如果用户已注销且user.is_authenticated=FALSE,如何防止用户直接访问main_app/url.py中的URL 请注意,我在Views.py中使用了基于类的视图。如果request.user.is_authenticated():不起作用,请参见以下内容: class EmployeeCreate(CreateView): model = Employee fields = ['first_name', 'last_name', 'role']

德扬戈

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

请注意,我在Views.py中使用了基于类的视图。如果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)

类LoginRequiredMixin解决了问题“不工作”怎么办?发生了什么?当请求不是帖子时,您希望它工作吗?您是否阅读了有关此问题的说明,包括?
是否经过身份验证
是一个属性,而不是一个方法,因此您不应该调用(
是否经过身份验证()
将引发异常).但是的,“不起作用”到底是什么?当我尝试注销时,它重定向到登录页面。但是当我直接访问此链接时,我仍然可以访问添加员工页面。这意味着用户可以直接访问urlsl.py中的URL,而无需登录。是的,因为您只保护了帖子,而不是GET。请参阅我发布的关于如何使用基于类的vie正确执行此操作的链接ws。