import os
from django.shortcuts import render
from django.contrib.admin.views.decorators import staff_member_required
from django.views.generic import View
from django.views.decorators.http import require_POST, require_GET
from django.contrib.auth.decorators import login_required # 登录装饰器
from django.utils.decorators import method_decorator from ..news.models import NewsCategory, News
from ..news.forms import NewsForm from utils import restful # Create your views here.
# staff_member_required(login_url) 用来验证is_staff(User表中的字段)是否为真,判断用户能否登入cms页面,
# login_url是不通过验证跳转, 前端通过login函数登录后的user.is_staff判断是否显示
@staff_member_required(login_url='/')
def index(request):
return render(request, 'cms/index.html') # method_decorator 使装饰器装饰在类上面(装饰器的类装饰器?) login_required 登陆验证,失败跳转
# despatch 类里面有多个方法(get,post).将这些方法都装饰在despatch中,(通过despatch方法确定出get or post 再由login_required装饰)。
@method_decorator(login_required(login_url='/account/login/'), name='dispatch')
class WriteNewsView(View):
def get(self, request):
categories = NewsCategory.objects.all()
return render(request, 'cms/write_news1.html', locals()) def post(self, request):
forms = NewsForm(request.POST)
if forms.is_valid():
cleaned_data = forms.cleaned_data
title = cleaned_data.get('title')
desc = cleaned_data.get('desc')
thumbnail = cleaned_data.get('thumbnail')
content = cleaned_data.get('content')
author = request.user category_id = cleaned_data.get('category')
category = NewsCategory.objects.get(id=category_id)
try:
News.objects.create(
title=title, desc=desc, thumbnail=thumbnail, content=content, category=category, author=author
)
return restful.ok()
except:
return restful.params_error("服务器gg")
error = forms.get_first_message()
return restful.params_error(error)

   # 简化版dispatch. 是View中的方法
def dispatch(self, request, *args, **kwargs):
if request.method == "GET":
return self.get(request)
elif request.method == "POST":
return self.post(request)

自定义django登录装饰器

def xfz_auth_required(func):
def wrapper(request, *args, **kwargs):
if request.user.is_authenticated:
return func(request, *args, **kwargs)
else:
if request.is_ajax():
return restful.params_error(message="请登陆")
return redirect('/account/login')
return wrapper

django-类装饰器method_decorator的更多相关文章

  1. Django - CBV装饰器实现用户登录验证

    一.使用Django自带的decorator 通常情况,使用 函数定义的view,可以直接使用 login_required 直接装饰 @login_required def index(reques ...

  2. django 使用装饰器验证用户登陆

    使用装饰器验证用户登陆,需要使用@method_decorator 首先需引用,method_decorator,并定义一个闭包 from django.utils.decorators import ...

  3. python装饰器2:类装饰器

    装饰器1:函数装饰器 装饰器2:类装饰器 装饰器3:进阶 本文是装饰器相关内容的第二篇,关于类装饰器. "类装饰器"有两种解读方式:用来装饰类的装饰器:类作为装饰器装饰其它东西.你 ...

  4. 类装饰器,元类,垃圾回收GC,内建属性、内建方法,集合,functools模块,常见模块

    '''''''''类装饰器'''class Test(): def __init__(self,func): print('---初始化---') print('func name is %s'%fu ...

  5. python 描述符 上下文管理协议 类装饰器 property metaclass

    1.描述符 #!/usr/bin/python env # coding=utf-8 # 数据描述符__get__ __set__ __delete__ ''' 描述符总结 描述符是可以实现大部分py ...

  6. 详解Python闭包,装饰器及类装饰器

    在项目开发中,总会遇到在原代码的基础上添加额外的功能模块,原有的代码也许是很久以前所写,为了添加新功能的代码块,您一般还得重新熟悉源代码,稍微搞清楚一点它的逻辑,这无疑是一件特别头疼的事情.今天我们介 ...

  7. [b0019] python 归纳 (五)_类装饰器

    总结: 类装饰器, 本质是一个函数,输入一个类,返回一个类 Case 1 啥都没做 def deco(in_class): return in_class @deco class Cat: def _ ...

  8. python带参数的类装饰器

    # -*- coding: utf-8 -*- # author:baoshan # 带参数的类装饰器(和不带参数的类装饰器有很大的不同) # 类装饰器的实现,必须实现__call__和__init_ ...

  9. typescript装饰器定义 类装饰器 属性装饰器 装饰器工厂

    /* 装饰器:装饰器是一种特殊类型的声明,它能够被附加到类声明,方法,属性或参数上,可以修改类的行为. 通俗的讲装饰器就是一个方法,可以注入到类.方法.属性参数上来扩展类.属性.方法.参数的功能. 常 ...

随机推荐

  1. ZOJ2345Gold Coins

    昨天做过一样的题: 平方和公式:n*(n+1)*(2n+1)/6 #include<cstdio> #include<cstdlib> #include<iostream ...

  2. 51Nod 1090: 3个数和为0

    1090 3个数和为0  基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等. ...

  3. test20180919 区间最大值

    题意 分析 我们将所有修改操作的左右端点都拿出来混合着排序. 然后扫描线一样扫描每个端点,维护一个堆储存当前最大值,然后就可以把这些修改操作分成O(m) 个不相交的区间,各自贡献独立. 复杂度为\(O ...

  4. sleep和 wait

  5. 时间操作(JavaScript版)—年月日三级联动(默认显示系统时间)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangshuxuncom/article/details/35263317         这个功能 ...

  6. Array对象(prototype)

  7. nyoj 素数距离

    素数距离问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度 ...

  8. Spark机器配置计算

    ● Based on the recommendations mentioned above, Let's assign 5 core per executors => --executor-c ...

  9. java 环境变量与安装目录

    JDK安装完成后有如下文件夹 bin:存放JDK的各种工具命令,如javac.java等命令. jre:运行java程序所必须的JRE环境 lib:JDK工具命令的实际执行程序,如tools.jar中 ...

  10. PHP图像处理

    1.创建画布: $img=imagescreatetruecolor(200,200); 创建颜色并填充 $red=imagecolorallocate($img,255,0,0);  //创建颜色 ...