为什么我的 Django request.method 是“GET”而不是“POST”?

Wei*_* Xu 3 python django django-forms

我在做一个预填充的表单时遇到了这个奇怪的问题。在我的模板中,表单方法明确说明为POST

<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">{% csrf_token %}
Run Code Online (Sandbox Code Playgroud)

但在我的视图函数中, 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))
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它会打印出GET. 以前有人遇到过这种奇怪的事情吗?

py_*_*dev 6

就我而言,我在发布时在 HTML 模板中的操作结束时遗漏了一个“/”。

  • 我花了 1 个小时试图理解为什么它不起作用。谢了哥们。 (3认同)