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

标签 python django django-forms

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

相关文章:

python - 网络上的PyAudio崩溃

python - 使用表单字段更新 Django 模型

python - 属性错误 : 'User_Profile' object has no attribute '__name__'

python - SQLAlchemy Unicode 难题

python - MySQLdb : how to connect with phpMyAdmin

python - EWOULDBLOCK 套接字编程错误

jquery - 单击弹出窗口会滚动回页面顶部 [Bootstrap and Django]

python - 如何在django中跨多个项目使用多数据库?

python - 为什么 DRF 可浏览 API 对每个实际请求的多个请求类型运行权限检查?

python - Django 中的 View 错误