Skip to main content
 首页 » 编程设计

python之为什么我的 Django request.method 是 'GET' 而不是 'POST'

2024年08月28日31txw1958

我在做预填充表单时遇到了这个奇怪的问题。 在我的模板中,表单方法明确表示为 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 模板中的操作结束时遗漏了一个“/”。