Python django中request.method出现问题,它无法识别POST,而是显示GET作为请求方法
我在django for Post and Get request和一些与URL相关的安全相关的东西上做了一些实践,所以我在一个html文件中制作了一个表单,其中我使用了方法作为Post,当它进入视图模板时,它将我的方法显示为Get,我无法理解,所以请解决我的问题谢谢!我附上这篇文章的截图供你参考 代码(HTML):- 因此,上面返回的是python代码的else部分,而不是if部分,我已经看到request.method函数将输出作为“GET” my django应用程序中的URL:Python django中request.method出现问题,它无法识别POST,而是显示GET作为请求方法,python,html,django,backend,Python,Html,Django,Backend,我在django for Post and Get request和一些与URL相关的安全相关的东西上做了一些实践,所以我在一个html文件中制作了一个表单,其中我使用了方法作为Post,当它进入视图模板时,它将我的方法显示为Get,我无法理解,所以请解决我的问题谢谢!我附上这篇文章的截图供你参考 代码(HTML):- 因此,上面返回的是python代码的else部分,而不是if部分,我已经看到request.method函数将输出作为“GET” my django应用程序中的URL: f
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")
发布代码,而不是图片。共享您的URL和呈现表单的view函数Sir我已经添加了所需的代码,请再次检查我编写的代码-代码(Python)该部分呈现表单基本上它不呈现它返回httpresponseNo它不工作它将执行else块而不在if中block@Varun方法将是POST,只有在你提交表格之后
"""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")