我有以下代码,在 Django 1.2.5 中运行良好:
from django.views.decorators.csrf import csrf_exempt
class ApiView(object):
def __call__(self, request, *args, **kwargs):
method = request.method.upper()
return getattr(self, method)(request, *args, **kwargs)
@csrf_exempt
class MyView(ApiView):
def POST(self):
# (...)
return HttpResponse(json.dumps(True), mimetype="text/javascript")
但是当我升级到 Django 1.4 时,我开始收到 403 禁止,并显示“CSRF 验证失败”消息。
为什么 @csrf_exempt 装饰器不起作用?
网址定义为:
from django.conf.urls.defaults import *
from django.views.decorators.csrf import csrf_exempt
import views
urlpatterns = patterns('',
url(r'^myview/(?P<parameter_name>[A-Za-z0-9-_]+)/$',
views.MyView(),
name="myproject-myapp-myview",
),
)