[英]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.