我的代码不断向我显示错误404:当前路径get与这些路径中的任何一个都不匹配

2024-03-29 00:47:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码 我不知道错误是从哪里来的。它告诉我 Django使用ehiz.URL中定义的URLconf,按以下顺序尝试了这些URL模式:

管理员/ [name='home'] add/[name='add'] 当前路径get与这些路径都不匹配

Views.py

from django.shortcuts import render


from django.http import HttpResponse

def home(request):
return render(request,'base.html',{'name':'Gael'})

def add(request):

val1 = int(request.GET['num1'])
val2 = int(request.GET['num2'])

res = val1 + val2

return render(request, 'result.html',{'result':res})

base.html

{% extends 'main.html' %}

{% block content %}
<h1>hello {{name}}</h1>

<form action="get">
<input type="text" name="num1" placeholder="Enter first number"><br>

<input type="text" name="num2" placeholder="enter second number"><br>

<input type="submit">


</form>


{% endblock %}

result.html

{% extends 'main.html' %}

{% block content %}

the result is : {{result}}

{% endblock %}

url.py

from django.urls import path

from . import views

urlpatterns = [
path('',views.home,name='home'),
path('add/',views.add,name='add')
]

1条回答
网友
1楼 · 发布于 2024-03-29 00:47:56

<form>中,您将method="…"action="…"属性混淆。method="…"指定HTTP请求类型,而action="…"指定请求将提交到哪个端点

因此,您应该与以下人员合作:

<form method="get" action="{% url 'add' %}">
    <input type="text" name="num1" placeholder="Enter first number"><be>
    <input type="text" name="num2" placeholder="enter second number"><be>
    <input type="submit">
</form>

不需要method="get",因为默认情况下,表单将通过GET请求提交

相关问题 更多 >