django-类装饰器method_decorator
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的更多相关文章
- Django - CBV装饰器实现用户登录验证
一.使用Django自带的decorator 通常情况,使用 函数定义的view,可以直接使用 login_required 直接装饰 @login_required def index(reques ...
- django 使用装饰器验证用户登陆
使用装饰器验证用户登陆,需要使用@method_decorator 首先需引用,method_decorator,并定义一个闭包 from django.utils.decorators import ...
- python装饰器2:类装饰器
装饰器1:函数装饰器 装饰器2:类装饰器 装饰器3:进阶 本文是装饰器相关内容的第二篇,关于类装饰器. "类装饰器"有两种解读方式:用来装饰类的装饰器:类作为装饰器装饰其它东西.你 ...
- 类装饰器,元类,垃圾回收GC,内建属性、内建方法,集合,functools模块,常见模块
'''''''''类装饰器'''class Test(): def __init__(self,func): print('---初始化---') print('func name is %s'%fu ...
- python 描述符 上下文管理协议 类装饰器 property metaclass
1.描述符 #!/usr/bin/python env # coding=utf-8 # 数据描述符__get__ __set__ __delete__ ''' 描述符总结 描述符是可以实现大部分py ...
- 详解Python闭包,装饰器及类装饰器
在项目开发中,总会遇到在原代码的基础上添加额外的功能模块,原有的代码也许是很久以前所写,为了添加新功能的代码块,您一般还得重新熟悉源代码,稍微搞清楚一点它的逻辑,这无疑是一件特别头疼的事情.今天我们介 ...
- [b0019] python 归纳 (五)_类装饰器
总结: 类装饰器, 本质是一个函数,输入一个类,返回一个类 Case 1 啥都没做 def deco(in_class): return in_class @deco class Cat: def _ ...
- python带参数的类装饰器
# -*- coding: utf-8 -*- # author:baoshan # 带参数的类装饰器(和不带参数的类装饰器有很大的不同) # 类装饰器的实现,必须实现__call__和__init_ ...
- typescript装饰器定义 类装饰器 属性装饰器 装饰器工厂
/* 装饰器:装饰器是一种特殊类型的声明,它能够被附加到类声明,方法,属性或参数上,可以修改类的行为. 通俗的讲装饰器就是一个方法,可以注入到类.方法.属性参数上来扩展类.属性.方法.参数的功能. 常 ...
随机推荐
- POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)
推荐网址,下面是别人的解题报告: http://www.cnblogs.com/chasetheexcellence/archive/2012/04/16/poj2441.html 里面有状态压缩论文 ...
- Hadoop学习笔记(1)(转)
Hadoop学习笔记(1) ——菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序. ...
- MySQL Transaction--RC和RR区别
在MySQL中,事务隔离级别RC(read commit)和RR(repeatable read)两种事务隔离级别基于多版本并发控制MVCC(multi-version concurrency con ...
- C# 使用oledb 方式连接本地或者远程oracel 数据库的方式
对于C# 进行oracle 数据库的开发来说使用oracle 提供的odp.net 方式是比较方便的,同时在性能以及兼容性也是比较好的 但是,对于不打算使用的,那么该如何使用oledb 进行连接 连接 ...
- nyoj 魔法少女
魔法少女 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 前些时间虚渊玄的巨献小圆着实火了一把. 在黑长直(小炎)往上爬楼去对抗魔女之夜时,她遇到了一个问题想请你帮忙. 因为 ...
- 让node支持es模块化(export、import)的方法
node版本v7.9.0,支持了大部分es6的功能,但还不支持es6模块化(export.import). 检测ES6 可以使用es-checker来检测当前Node.js对ES6的支持情况. 使用命 ...
- tomcat和servlet关系
https://blog.csdn.net/weixin_30531261/article/details/78574410 图不错:https://www.cnblogs.com/fengli999 ...
- JS给HTML5页面<Select></Select>绑定选中项
获取选中值: function setApprovalPersonName() { var name = $("#approvalPersion").find("opti ...
- 解决webpack不是内部命令
在指定路径下安装webpack npm install webpack --save-dev 但是报”不是内部命令错误" 解决方法:安装全局webpack npm install web ...
- C#利用zxing.net操作二维码和条形码
下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: 代码: usin ...