Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2225523
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2130
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-31 14:30
个人简介

自己慢慢积累。

文章分类

全部博文(287)

分类: Web开发

2016-11-16 15:29:57

转自:http://blog.csdn.net/chen_jint/article/details/12956797

解决步骤:

1〉django工程settings.py

[python] view plain copy
 print?
  1. MIDDLEWARE_CLASSES = (  
  2.     'django.middleware.common.CommonMiddleware',  
  3.     'django.contrib.sessions.middleware.SessionMiddleware',  
  4.     'django.middleware.csrf.CsrfViewMiddleware',#确认存在  
  5.     'django.contrib.auth.middleware.AuthenticationMiddleware',  
  6.     'django.contrib.messages.middleware.MessageMiddleware',  
  7.     # Uncomment the next line for simple clickjacking protection:  
  8.     # 'django.middleware.clickjacking.XFrameOptionsMiddleware',  
  9. )  

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

[html] view plain copy
 print?
  1. <form action="." method="post">{% csrf_token %}  
3〉django工程views.py

[python] view plain copy
 print?
  1. from django.shortcuts import render_to_response  
  2. from django.template import RequestContext  
  3.   
  4. def some_view(request):  
  5.     # ...  
  6.     return render_to_response('my_template.html',  
  7.                               my_data_dictionary,  
  8.                               context_instance=RequestContext(request))  

有疑问请戳Cross Site Request Forgery protection

P.S如果要屏蔽CSRF

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

[python] view plain copy
 print?
  1. #'django.middleware.csrf.CsrfViewMiddleware'  
方法2:django工程views.py添加屏蔽装饰器

[python] view plain copy
 print?
  1. from django.views.decorators.csrf import csrf_exempt   
  2. @csrf_exempt  
  3. def some_view(request):  
  4.     #...  
阅读(4329) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~