I met this strange problem when doing a pre-populated form. In my template, the form method is clearly stated as 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
.
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
. 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. When you fill the form values and submit the form(with post method) ie insert/update remote data, the request method is POST.
So, in your code when you print request.method
, the output is GET when you are loading the form. 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>
elementaction
property on your <form>
elementtype="submit"
to your <button>
elementPs. Don't forget to add {% csrf_token %}
after your <form>
.
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.
Sorry, I get misunderstood when again I face the same problem then I got actual solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.