[英]Why does my Django request.method is 'GET' not 'POST'?
I met this strange problem when doing a pre-populated form.我在做一个预填充的表单时遇到了这个奇怪的问题。 In my template, the form method is clearly stated as
POST
:在我的模板中,表单方法明确声明为
POST
:
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">{% csrf_token %}
But in my view function, the request.method turns out to be GET
.但在我的视图函数中, request.method 结果是
GET
。
Below is my view function:下面是我的视图函数:
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))
When I run it, it prints out GET
.当我运行它时,它会打印出
GET
。 Anyone met this odd before?以前有人遇到过这种奇怪的事情吗?
就我而言,我在发布时在 HTML 模板中的操作结束时遗漏了一个“/”。
When you are loading the form and retrieving remote data by hitting a url, the request method is GET.当您通过点击 url 加载表单并检索远程数据时,请求方法是 GET。 When you fill the form values and submit the form(with post method) ie insert/update remote data, the request method is POST.
当您填写表单值并提交表单(使用 post 方法)即插入/更新远程数据时,请求方法为 POST。
So, in your code when you print request.method
, the output is GET when you are loading the form.因此,在您打印
request.method
代码中,当您加载表单时,输出为 GET。 This has nothing to do with your pre-populated form.这与您预先填写的表格无关。
每次我使用action=""
提交表单时,我都会收到 GET 响应,但是一旦我填写了实际的 URL action="/client/"
它就会作为 POST 完成。
Here are the steps I needed so far to solve this kind of error:以下是我目前解决此类错误所需的步骤:
method="post"
to your <form>
elementmethod="post"
添加到您的<form>
元素action
property on your <form>
element<form>
元素的action
属性的 url 末尾添加缺少的“/”type="submit"
to your <button>
elementtype="submit"
添加到您的<button>
元素Ps.附言。 Don't forget to add
{% csrf_token %}
after your <form>
.不要忘记在
<form>
之后添加{% csrf_token %}
。
I am also facing this problem but, In my case, in input type, I have written type =" button" when I change it to type="submit" it get resolved.我也面临这个问题,但是,在我的情况下,在输入类型中,当我将其更改为 type="submit" 时,我写了 type="button" 它得到解决。
Sorry, I get misunderstood when again I face the same problem then I got actual solution.对不起,当我再次遇到同样的问题然后我得到了实际的解决方案时,我被误解了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.