日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

ajax post forbidden,POST方法总是返回403 Forbidden

發布時間:2025/3/20 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。