验证用户是否登录
# 开始在用户登录的时候验证结束后login登录
# request.user.is_authenticated()
{% if request.user.is_authenticated %}
# 函数用这样的装饰器
from django.contrib.auth.decorators import login_required
@login_required
# 类用这样的基础登录函数
(minxi表示一些基础的类)
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
class LoginRequiredMixin(object):
@method_decorator((login_required(login_url='/login/')))
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
在视图中应该这样写
class CourseCommentView(LoginRequiredMixin, View)