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. 【JUnit】@Test 报错,"Test cannot be resolved to a type"

    想用单元测试 JUnit 单元测试下写好的方法,发现写 @Test 标签报错了,"Test cannot be resolved to a type" 原来是项目没有导入 JUni ...

  2. IO练习--按字节截取字符串

    * 在Java中字符串“abcd”和字符串“ab你好”都是4个字符, * 但是字节数不同,因为GBK中一个汉字占两个字节 * 定义一个方法用来按字节数截取字符串. * 如:对于“ab你好”,取3个字节 ...

  3. java面试题12

    1.  jsp与servlet的区分? 答:Servlet和JSP都是基于java语言上的动态网页技术,Servlet程序其实就是java程序,只不过它所使用的类库为JAVA Servlet API, ...

  4. StyleCop 是什么,可以帮助团队带来什么价值?

    StyleCop 本质上是一个 C# 源代码规则分析器,可以帮助团队成员强制执行一组代码样式和一致性规则. 本文将简述 StyleCop 以及它能为团队带来的价值. 本文内容 StyleCop 是什么 ...

  5. mysql 常用linux命令

    ★ 数据导出命令 D:\Program Files\MySQL\MySQL Server 5.6.39\bin 导出:  mysqldump -u root -p cela_sub > D:/d ...

  6. Hadoop storm大数据分析 知识体系结构

    最近工作工作有用到hadoop 和storm,最近看到一个网站上例句的hadoop 和storm的知识体系.所以列出来供大家了解和学习.来自哪个网站就不写了以免以为我做广告额. 目录结构知识点还是挺全 ...

  7. vector容器的用法以及动态数组

    vector容器不必去管大小 string申明的数组已经是动态的了 若是int类型的话,需要 cin>>N: int a[N]会出错 ,必须是int *p = new int[N] 然后再 ...

  8. sql server 表变量存储临时查询数据

    对于使用sql server 编写存储过程或者类似的sql 查询的时候我们使用表变量进行临时数据的存储,可以方便我们进行下来的数据处理 表变量的使用类似如下: declare @userinfo ta ...

  9. BAT调用7z压缩程序

    @echo offset zip=C:\Program Files\7-Zip\7z.exeset timestamp=%date:~6,4%-%date:~0,2%-%date:~3,2%set d ...

  10. Linux下的Nginx、php、mysql、apache部署

    待补充,先搞几个博客链接: https://www.cnblogs.com/Candies/p/8282934.html http://sujianjob.com/2017/12/18/yum%E5% ...