django显示数据前验证登录

1. 作用于函数:

@login_required(login_url='/index/')   

2. 作用于类:

from django.contrib.auth.mixins import LoginRequiredMixin

  class MyView(LoginRequiredMixin, View):

    login_url = '/login/'

    redirect_field_name = 'redirect_to'

3. 作用于类:

官方文档:https://docs.djangoproject.com/zh-hans/3.0/topics/auth/default/

对某个特定的视图函数加上登录验证(其中的login_required是一个装饰器,若不理解装饰器的使用,请参看https://www.runoob.com/w3cnote/python-func-decorators.html。注意:装饰器中的@符号, 那只是用一个简短的方式来生成一个被装饰的函数)

原文地址:https://www.cnblogs.com/xiaohaodeboke/p/12797415.html