【问题标题】:Problem with request.method in django which is not recognising POST instead it is showing GET as request methoddjango 中 request.method 的问题,它无法识别 POST,而是将 GET 显示为请求方法
【发布时间】:2020-09-03 10:43:40
【问题描述】:

我正在使用 django 为 Post 和 Get 请求以及一些与 url 相关的安全相关的东西做一些实用的事情,所以我在一个 html 文件中制作了一个表单,我在其中使用了方法作为 post,然后当它进入视图模板时它显示我的方法为 GET 我无法弄清楚,所以请解决我的问题,谢谢!我附上此帖子的屏幕截图供您参考。

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

Here I have attached my browser screen which shows that my method is using GET request

代码(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 %}

代码(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))

所以上面是返回 python 代码的 else 部分而不是 if 部分,我看到 request.method 函数将输出作为“GET”

我的 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"),
    ]

我的主项目中的 URL:-

"""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')),
]

【问题讨论】:

  • 发布您的代码,而不是图片。 stackoverflow.com/help/how-to-ask
  • 分享您的网址和呈现表单的视图功能
  • 先生,我已经添加了所需的代码,请检查一次
  • 先生,我编写的代码 - 该部分的代码(Python)基本上呈现表单它不呈现它返回httpresponse

标签: python html django backend


【解决方案1】:

在您看来,请执行以下操作:

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

【讨论】:

  • 不,它不工作它会被 else 阻塞而不是 if 阻塞
  • @Varun 方法将是 POST,只有在您提交表单之后
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 2015-08-06
  • 2021-11-16
  • 2012-06-17
  • 2012-05-27
  • 2019-12-18
相关资源
最近更新 更多