简体   繁体   English

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

[英]Why does my Django request.method is 'GET' not 'POST'?

I met this strange problem when doing a pre-populated form.我在做一个预填充的表单时遇到了这个奇怪的问题。 In my template, the form method is clearly stated as POST :在我的模板中,表单方法明确声明为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 .但在我的视图函数中, request.method 结果是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 .当我运行它时,它会打印出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.当您通过点击 url 加载表单并检索远程数据时,请求方法是 GET。 When you fill the form values and submit the form(with post method) ie insert/update remote data, the request method is POST.当您填写表单值并提交表单(使用 post 方法)即插入/更新远程数据时,请求方法为 POST。

So, in your code when you print request.method , the output is GET when you are loading the form.因此,在您打印request.method代码中,当您加载表单时,输出为 GET。 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:以下是我目前解决此类错误所需的步骤:

  1. Add method="post" to your <form> elementmethod="post"添加到您的<form>元素
  2. Add a missing "/" at the end of the url at the action property on your <form> element<form>元素的action属性的 url 末尾添加缺少的“/”
  3. Add a type="submit" to your <button> elementtype="submit"添加到您的<button>元素

Ps.附言。 Don't forget to add {% csrf_token %} after your <form> .不要忘记在<form>之后添加{% csrf_token %}

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.我也面临这个问题,但是,在我的情况下,在输入类型中,当我将其更改为 type="submit" 时,我写了 type="button" 它得到解决。

Sorry, I get misunderstood when again I face the same problem then I got actual solution.对不起,当我再次遇到同样的问题然后我得到了实际的解决方案时,我被误解了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:为什么我的表单发送POST数据却返回request.method GET? - Django: Why is my form sending POST data but returning request.method GET? request.method == &#39;POST&#39; 在 Django 中不起作用 - request.method == 'POST' is not working in Django Django "如果 request.method == 'POST':" 返回 False - Django "if request.method == 'POST':" returns False Django Test request.method ==&#39;POST&#39;不起作用 - Django Test request.method == 'POST' not working Django request.method =&#39;GET&#39;无法正常工作 - Django request.method = 'GET' not working django 中 request.method 的问题,它无法识别 POST 而是将 GET 显示为请求方法 - Problem with request.method in django which is not recognising POST instead it is showing GET as request method 为什么request.files中的request.method ==&#39;POST&#39;和&#39;photo&#39; - Why is if request.method == 'POST' and 'photo' in request.files Django 文件上传表单:如果 request.method==“POST” 失败 - Django File Upload form: if request.method==“POST” fails 烧瓶默认运行request.method&#39;POST&#39;而不是&#39;GET&#39; - Flask running request.method 'POST' by default instead of 'GET' 403 Forbidden和request.method在Django中显示GET - 403 Forbidden and request.method showing GET in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM