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装饰器定义 类装饰器 属性装饰器 装饰器工厂
/* 装饰器:装饰器是一种特殊类型的声明,它能够被附加到类声明,方法,属性或参数上,可以修改类的行为. 通俗的讲装饰器就是一个方法,可以注入到类.方法.属性参数上来扩展类.属性.方法.参数的功能. 常 ...
随机推荐
- LDA模型应用实践-希拉里邮件主题分类
#coding=utf8 import numpy as np import pandas as pd import re from gensim import corpora, models, si ...
- IIS 使用 HTTP/2
什么叫HTTP/2? HTTP 2.0即超文本传输协议 2.0,是下一代HTTP协议.是由互联网工程任务组(IETF)的Hypertext Transfer Protocol Bis (httpbis ...
- 《DSP using MATLAB》 Problem 4.9
代码: %% ---------------------------------------------------------------------------- %% Output Info a ...
- python 取整
1.向下取整 向下取整直接用内建的 int() 函数即可: >>> a = 3.75 >>> int(a) 3 2.四舍五入 对数字进行四舍五入用 round() ...
- Hadoop伪分布模式操作
http://blog.csdn.net/wangloveall/article/details/20195813 摘要:本文介绍Hadoop伪分布模式操作,适合于Hadoop学习.开发和调试. 关键 ...
- SQL Server 中关于EXCEPT和INTERSECT的用法
熟练使用SQL Server中的各种用法会给查询带来很多方便.今天就介绍一下EXCEPT和INTERSECT.注意此语法仅在SQL Server 2005及以上版本支持. EXCEPT是指在第一个集合 ...
- log4net保存到数据库系列一:WebConfig中配置log4net
园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志 一.WebConfig中配置log4net 二.独立配置文件中配置log4net ...
- RabbitMQ之监控
RabbitMQ作为一个工业级的消息中间件,肯定是缺少不了监控的,RabbitMQ提供了WEB版的页面监控(访问地址:http://xxx.xxx.xxx.xxx:15672/,默认端口号是15672 ...
- HTMLCanvasElement.toBlob() 兼容性及使用
toBlob 兼容性: 在最新版chrome和firefox中能正常使用,在Safari中报错:没有这个函数 规避方法: 不使用toBlob,使用toDataURL()将file转成base64编码, ...
- python下爬某个网页的图片
#coding=utf-8 import re import urllib def getHtml(url): #获取url对应得源码 page = urllib.urlopen(url) html ...