如何解决Django中的request.method问题无法识别POST,而是将GET显示为请求方法
我在django中进行了一些实用的Post and Get请求以及一些与URL相关的安全性相关的工作,因此我在html文件中制作了一个表单,在其中我已将method用作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')),]
解决方法
在您看来,请执行以下操作:
def check(request):
if request.method == 'POST':
return HttpResponse("This is POST request")
else:
return render(request,"form.html")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。