Django
如何防止用户直接访问 main_app/url 中的 URL.py如果用户已注销并且user.is_authenticated = FALSE
请注意,我在 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)
class LoginRequiredMixin 解决了 https://docs.djangoproject.com/en/2.0/topics/auth/default/#the-loginrequired-mixin
的问题