django如何给类视图加装饰器?

导读:今天首席CTO笔记来给各位分享关于django如何给类视图加装饰器的相关内容,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

Django报错:AttributeError: \'function\' object has no attribute \'as_view\'

在学习Django视图策略的时候,使用基于类的视图 (CBV),遇到了一个问题:

先简单介绍一下CBV:

我的视图如下:

url:

最终在Stack Overflow上找到了答案,在这个类上不能使用@login_required这个装饰器,而需要使用method_decorator,并传递一个装饰器(或一个装饰器列表)并告诉应该装饰哪个类。在 CBV 中,装饰 调度类 是很常见的。它是一个Django内部使用的方法(在 View 类中定义)。所有的请求都会经过这个类,所以装饰它会相对安全。如下,注意导入模块from django.utils.decoratorsimport method_decorator:

这样问题就解决了,这里贴上Stack Overflow原回答链接: Django/python: \'function\' object has no attribute \'as_view\' - Stack Overflow

Python如何合并多个装饰器?教你几个小技巧

Python如何合并多个装饰器?教你几个小技巧

django程序,需要写很多api,每个函数都需要几个装饰器 ,例如

复制代码 代码如下:

@csrf_exempt

@require_POST

def foo(request):

pass

既然那么多个方法都需要写2个装饰器,或者多个,有啥办法把多个合并成一行呢?

上面的函数执行过程应该是

复制代码 代码如下:

csrf_exempt(require_POST(foo))

修改成

复制代码 代码如下:

def compose(*funs):

def deco(f):

for fun in reversed(funs):

f = fun(f)

return f

return deco

函数改写成

复制代码 代码如下:

@compose(csrf_exempt, require_POST)

def foo(request):

pass

参考:

Can I combine two decorators into a single one in Python

;

\"CSRF token missing or incorrect.\"的解决办法怎么解决

CSRF token missing or incorrect.\"的解决方法.

现象:

Forbidden (403)

CSRF verification failed. Request aborted.

Help

Reason given for failure:

CSRF token missing or incorrect.

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django\'s CSRF mechanism has not been used correctly. For POST forms, you need to ensure:

Your browser is accepting cookies.

The view function uses RequestContext for the template, instead of Context.

In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.

If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.

You\'re seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.

You can customize this page using the CSRF_FAILURE_VIEW setting.

解决步骤:

1〉django工程settings.py

MIDDLEWARE_CLASSES = (

\'django.middleware.common.CommonMiddleware\',

\'django.contrib.sessions.middleware.SessionMiddleware\',

\'django.middleware.csrf.CsrfViewMiddleware\',#确认存在

\'django.contrib.auth.middleware.AuthenticationMiddleware\',

\'django.contrib.messages.middleware.MessageMiddleware\',

# Uncomment the next line for simple clickjacking protection:

# \'django.middleware.clickjacking.XFrameOptionsMiddleware\',

)

2〉html中的form添加模板标签{% csrf_token %}

form action=\".\" method=\"post\"{% csrf_token %}

3〉django工程views.py

from django.shortcuts import render_to_response

from django.template import RequestContext

def some_view(request):

# ...

return render_to_response(\'my_template.html\',

my_data_dictionary,

context_instance=RequestContext(request))

有疑问请戳Cross Site Request Forgery protection

P.S如果要屏蔽CSRF

方法1:注释掉django工程settings.py中

#\'django.middleware.csrf.CsrfViewMiddleware\'

方法2:django工程views.py添加屏蔽装饰器

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt

def some_view(request):

#...

django如何给类视图加装饰器?  第1张

Django里面怎么实现数据库视图啊 就是虚拟表

正经回答:先在数据库中建立好视图,然后django中建立对应的model。表所对应的类下面再建立一个Meta类,大致如下

class ViewModel(models.Model):

    \"\"\"这个model类对应你所建立好的视图\"\"\"

    class Meta(object):

        \"\"\"同理,该方法可用于使用mysql中任何已有的表,不仅是视图\"\"\"

        db_table = \'your_view\' #显式指定表名,也就是你建立的视图的名字

        managed = false #默认是ture,设成false django将不会执行建表和删表操作

    # 建立字段间的映射

    #  需要注意的是,必须设一个字段为主键

    #  不然django会自动创建一个id字段为主键,引发错误

百度知道越来越辣鸡了,全是答非所问的。

结语:以上就是首席CTO笔记为大家介绍的关于django如何给类视图加装饰器的全部内容了,希望对大家有所帮助,如果你还想了解更多这方面的信息,记得收藏关注本站。

以上内容为新媒号(sinv.com.cn)为大家提供!新媒号,坚持更新大家所需的互联网后端知识。希望您喜欢!

版权申明:新媒号所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,不声明或保证其内容的正确性,如发现本站有涉嫌抄袭侵权/违法违规的内容。请发送邮件至 k2#88.com(替换@) 举报,一经查实,本站将立刻删除。

(0)
上一篇 2023-09-23 13:57
下一篇 2023-09-23 13:58

相关推荐

发表回复

登录后才能评论