【发布时间】:2021-01-12 03:11:01
【问题描述】:
http://127.0.0.1:8000/tasks 工作正常,但是当我添加任务并提交时,我收到了 404,
错误:使用 mysite.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:
admin/
polls/
newyear/
tasks/ [name='index']
tasks/ add [name='add']
The current path, tasks/{ % url 'add' % }, didn't match any
of these
mysite\tasks\urls.py
from django.urls import path,include
from . import views
urlpatterns = [
path("", views.index, name='index'),
path("add", views.add, name='add')]
mysite\mysite\urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/',include('polls.urls')),
path('newyear/', include('newyear.urls')),
path('tasks/', include('tasks.urls'))]
mysite\tasks\views.py
from django.shortcuts import render
tasks = ["foo", "bar", "baz"]
def index(request):
return render(request, "tasks/index.html", {
"tasks": tasks
})
def add(request):
return render(request, "tasks/add.html")
)
mysite\tasks\templates\tasks\index.html
{% extends "tasks/layout.html" %}
{% block body %}
<h1>
Tasks
</h1>
<ul>
{%for task in tasks%}
<li> {{task}}</li>
{%endfor%}
</ul>
<a href="{% url 'add' %}">Add a New Task</a>
{% endblock %}
mysite\tasks\templates\tasks\add.html
{% extends "tasks/layout.html" %}
{% block body %}
<h1>
Add Task
</h1>
<form action= "{ % url 'add' % }" method="post">
<input type="text" name="task">
<input type="submit">
</form>
<a href= "{% url 'index' %}">View Tasks</a>
{% endblock %}
【问题讨论】:
-
因为您没有编写发布请求的代码。
-
path("add/", views.add, name='add')] 斜杠有帮助吗?
-
@SuryaPratapRana 请详细说明?
-
@AlexeyPopov 不
-
@KwakuE.Biney tasks/{ % url 'add' % } 是确切的 request.path 吗?您的“添加”名称似乎没有被反转
标签: python django django-forms