简体   繁体   中英

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! 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

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

Code(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) :-

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"

URLS in my django app:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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