登录装饰器的使用
1. FBV
1. settings.py配置
LOGIN_URL = '/login/'
2. views.py中
from django.contrib.auth.decorators import login_required
@login_required
def xxx(request):
return xxx
2. CBV
1. 同样要在settings.py中配置
2. 两种方法
方法一:
直接在urls.py中加装饰
url(r'^$', login_required(views.UserInfoView.as_view()), name='user'),
方法二:
定义一个类,用视图类继承
utils/mixin.py
from django.contrib.auth.decorators import login_required
class LoginRequiredMixin(object):
@classmethod
def as_view(cls, **initkwargs):
view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
return login_required(view)
然后在视图类里面继承
class UserOrderView(LoginRequiredMixin, View):
def get(self, request):
return render(request, 'user_center_order.html', {'page':'order'})