簡體   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

我正在使用 django 為 Post 和 Get 請求以及一些與 urls 相關的安全相關的東西做一些實踐,所以我在一個 html 文件中創建了一個表單,在其中我使用了方法作為 post,當它進入視圖模板時,它就會顯示我的 GET 方法我無法弄清楚,所以請解決我的問題,謝謝! 我附上這篇文章的截圖供您參考。

此代碼適用於我使用方法作為帖子的表單頁面

我附上了我的“檢查功能”,它總是轉到 else 塊而不是 if 塊

在這里,我附上了我的瀏覽器屏幕,顯示我的方法正在使用 GET 請求

代碼(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"),
    ]

我的主要項目中的網址:-

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

在您看來,請執行以下操作:

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