为什么我的Django请求方法是'GET'而不是'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
。
有没有人遇到过这种奇怪的情况?
5 个回答
1
每次我提交表单时,如果把 action=""
留空,我总是收到一个GET响应。但是一旦我填写了实际的URL action="/client/"
,它就变成了POST请求。
3
当你在加载表单并通过访问一个网址来获取远程数据时,使用的请求方式是GET。
而当你填写表单的内容并提交表单(使用POST方式),也就是插入或更新远程数据时,使用的请求方式是POST。
所以,在你的代码中,当你打印 request.method
的时候,加载表单时输出的是GET。这和你预先填好的表单没有关系。
6
在我的情况下,我在HTML模板的动作部分最后漏掉了一个“/”,导致了问题。