![](/img/trans.png)
[英]Django: Why is my form sending POST data but returning request.method GET?
[英]Why does my Django request.method is 'GET' not 'POST'?
我在做一個預填充的表單時遇到了這個奇怪的問題。 在我的模板中,表單方法明確聲明為POST
:
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">{% csrf_token %}
但在我的視圖函數中, request.method 結果是GET
。
下面是我的視圖函數:
def editProfile(request,template_name):
theprofile = request.user.profile
print theprofile.fullname
notificationMSG = ''
print request.method
if request.method == 'POST':
form = UserProfileForm(request.POST,request.FILES, instance=theprofile)
if form.is_valid():
form.save()
notificationMSG = "success!"
else:
form = UserProfileForm()
print "error"
dic = {'form':form,
'notificationMSG':notificationMSG}
return render_to_response(template_name, dic, context_instance=RequestContext(request))
當我運行它時,它會打印出GET
。 以前有人遇到過這種奇怪的事情嗎?
就我而言,我在發布時在 HTML 模板中的操作結束時遺漏了一個“/”。
當您通過點擊 url 加載表單並檢索遠程數據時,請求方法是 GET。 當您填寫表單值並提交表單(使用 post 方法)即插入/更新遠程數據時,請求方法為 POST。
因此,在您打印request.method
代碼中,當您加載表單時,輸出為 GET。 這與您預先填寫的表格無關。
每次我使用action=""
提交表單時,我都會收到 GET 響應,但是一旦我填寫了實際的 URL action="/client/"
它就會作為 POST 完成。
以下是我目前解決此類錯誤所需的步驟:
method="post"
添加到您的<form>
元素<form>
元素的action
屬性的 url 末尾添加缺少的“/”type="submit"
添加到您的<button>
元素附言。 不要忘記在<form>
之后添加{% csrf_token %}
。
我也面臨這個問題,但是,在我的情況下,在輸入類型中,當我將其更改為 type="submit" 時,我寫了 type="button" 它得到解決。
對不起,當我再次遇到同樣的問題然后我得到了實際的解決方案時,我被誤解了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.