ajax post forbidden,POST方法总是返回403 Forbidden
我讀過Django - CSRF驗證失敗,以及與django和POST方法相關的幾個問題(和答案).其中一個最好但不能正常工作的答案是/sf/ask/17360801/
所有批準的答案都表明至少有三件事:
使用RequestContext作為render_to_response_call的第三個參數
使用POST方法在每個表單中添加{%csrf_token%}
檢查settings.py中的MIDDLEWARE_CLASSES
我完全按照建議做了,但錯誤仍然出現.我使用django 1.3.1(來自ubuntu 12.04存儲庫)和python 2.7(默認來自ubuntu)
這是我的觀點:
# Create your views here.
from django.template import RequestContext
from django.http import HttpResponse
from django.shortcuts import render_to_response
from models import BookModel
def index(request):
return HttpResponse('Welcome to the library')
def search_form(request):
return render_to_response('library/search_form.html')
def search(request):
if request.method=='POST':
if 'q' in request.POST:
q=request.POST['q']
bookModel = BookModel.objects.filter(title__icontains=q)
result = {'books' : bookModel,}
return render_to_response('library/search.html', result, context_instance=RequestContext(request))
else:
return search_form(request)
else:
return search_form(request)
這是我的模板(search_form.html):
{% extends "base.html" %}
{% block content %}
{% csrf_token %}
{% endblock %}
我重新啟動了服務器,但403禁止錯誤仍然存??在,告訴CSRF驗證失敗.
我有兩個問題:
如何解決這個錯誤?
為什么在django中制作"POST"這么難,我的意思是有任何特定的理由讓它變得如此冗長(我來自PHP,以前從未發現過這樣的問題)?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的ajax post forbidden,POST方法总是返回403 Forbidden的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器监控报警系统软件设计,船舶机舱监控
- 下一篇: exchange server 2003