简体   繁体   English

django 中 request.method 的问题,它无法识别 POST 而是将 GET 显示为请求方法

[英]Problem with request.method in django which is not recognising POST instead it is showing GET as request method

I was doing some practical with django for Post and Get request and some security related stuff related to urls so I made a form in a html file in which I have used method as post and when it is going inside the views template then it is showing my method as GET I am not able to figure it out so please solve my issue thank you!我正在使用 django 为 Post 和 Get 请求以及一些与 urls 相关的安全相关的东西做一些实践,所以我在一个 html 文件中创建了一个表单,在其中我使用了方法作为 post,当它进入视图模板时,它就会显示我的 GET 方法我无法弄清楚,所以请解决我的问题,谢谢! I am attaching screenshot with this Post for your reference.我附上这篇文章的截图供您参考。

This code is for my form page in which I have used method as post此代码适用于我使用方法作为帖子的表单页面

I have attached my 'check function' in which it is always going to the else block and not if block我附上了我的“检查功能”,它总是转到 else 块而不是 if 块

Here I have attached my browser screen which shows that my method is using GET request在这里,我附上了我的浏览器屏幕,显示我的方法正在使用 GET 请求

Code(HTML) :-代码(HTML):-

{% block body %}
<form action="check" method="post">
   {% csrf_token %}
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" name="password">
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>
    {% endblock body %}

Code (Python) :-代码(Python):-

def check(request):
    if request.method == 'POST':
        return HttpResponse("Now url is not having your E-mail and Password")
    else:
        return HttpResponse("Using url (you can also see your email and password in url) we can easily delete your "
                            "account which is a major flaw in html GET request which "
                            "is set by default in form tag! Method used: {}".format(request.method))

So The above is returning the else part of the python code instead of if part and I have seen the request.method function is giving the output as "GET"所以上面是返回python代码的else部分而不是if部分,我已经看到request.method函数将输出作为“GET”

URLS in my django app:我的 Django 应用中的 URL:

from django.urls import path
from . import views

    urlpatterns = [
        path('', views.index, name="index"),
        path('find', views.find, name="find"),
        path('form_index/', views.form_index, name="form_index"),
        path('form_index/check/', views.check, name="check"),
    ]

URLS in my main project :-我的主要项目中的网址:-

"""testing URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('sse.urls')),
]

in your views, do something like:在您看来,请执行以下操作:

def check(request):
    if request.method == 'POST':
        return HttpResponse("This is POST request")
    else:
        return render(request, "form.html")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM