• Forbidden (403) CSRF verification failed. Request aborted.


      学习django1.4,没有中文资料,只能慢慢啃官方教程。

      最近被这个问题纠结几天了,一直没有解决,虽然用的是业余时间,但是这个问题不解决,后面的教程感觉都做不下去了。这个问题是这样长生的:我用post方法提交表单,出现标题那个错误,django错误系统也给出了三个途径,除了第三个没有完全懂之外,其它的都试了好多遍了。最后却发现在setting里面把CSRF的注释掉就正确了。还是把django的提示贴出来吧(不过没有用哦):

      

    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.

      最后成功的解决办法:在setting.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',
    )

      这是我自学python的第一个帖子吧,也算是开了第一篇了,虽然简单,自己感觉还是蛮艰难的。

  • 相关阅读:
    redhat 5 中文乱码
    生成树
    交换机端口模式
    链路聚合
    AP注册
    信息收集
    Python 25 Django跨域请求
    Python 24 Django之csrf中间件
    Python 23 Django基础
    Python 21 Flask(三)第三方组件
  • 原文地址:https://www.cnblogs.com/slider/p/2549955.html
Copyright © 2020-2023  润新知