我在做预填充表单时遇到了这个奇怪的问题。
在我的模板中,表单方法明确表示为 POST
:
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">{% csrf_token %}
但在我的 View 函数中,request.method 结果是GET
。
下面是我的 View 函数:
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 模板中的操作结束时遗漏了一个“/”。
关于python - 为什么我的 Django request.method 是 'GET' 而不是 'POST'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22046588/