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. 【转载】Python字符串操作之字符串分割与组合

    1. str.split():字符串分割函数 通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. 语法: str.split(s, num)[n] 参数说明: s:表示指定的分隔符,不写的话, ...

  2. CentOS7防止CC攻击防止SSH爆破采取的措施

    准备工作 1.检查firewalld是否启用 #如果您已经安装iptables建议先关闭 service iptables stop #查看firewalld状态 firewall-cmd --sta ...

  3. $.ajax()方法详解--极快瑞中的阿贾克斯函数

    ajax() 函数和那些abs()函数 sin()这些函数都是底层实现好了,封装好了,可以直接拿来用的 使用者只需要传入参数,然后就可以得到相应的结果 $.ajax({name:value, name ...

  4. 4.图像sensor的特性和驱动解析

    修改 摄像头SDK中支持的sensor需要做的事 例如:ar0130 --> ov9712 1.修改加载load3518e脚本的参数 vi /etc/profile ./load3518e -i ...

  5. c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息

    我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统名称.物理内存等. 首先需要引入命名空间: us ...

  6. protobuf 协议 windows 下 C++ 环境搭建

    1. 下载protobuf https://code.google.com/p/protobuf/downloads/list Protocol Buffers 2.5.0 full source - ...

  7. O(n)空间复杂度,打印杨辉三角形的前n行

    做小米的笔试题,给出一个整数n,求出它在杨辉三角形中第一次出现的行号. 想了半天,只能暴力法,从第1行开始找,一直找到第n行,若找得到则返回行号,若找不到则返回n+1(因为第n+1行第2列肯定是n). ...

  8. Modbus tcp 格式说明 通讯机制 附C#测试工具用于学习,测试

    前言: 之前的博客介绍了如何用C#来读写modbus tcp服务器的数据,文章:http://www.cnblogs.com/dathlin/p/7885368.html 当然也有如何创建一个服务器文 ...

  9. FPGA该如何应对ASIC的大爆发?

    有人认为,除了人才短缺.开发难度较大,相比未来的批量化量产的ASIC芯片,FPGA在成本.性能.功耗方面仍有很多不足.这是否意味着,在ASIC大爆发之际,FPGA将沦为其“过渡”品的命运? 安路科技市 ...

  10. java web中获取各种路径

    一.获取项目路径: 1) String path = request.getContextPath() :// /TestMyEclipseString basePath = request.getS ...