Django 404错误,即使我已经创建了路径和视图

2024-09-28 22:18:54 发布

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

我才刚开始学Django。 我在另一个名为“djangoTest”的应用程序中创建了一个名为“flavo”的简单web子应用程序 当我运行http://127.0.0.1:8000/flavo 它可以正确显示 Hello, World! 然后,当我运行http://127.0.0.1:8000/flavo/a时,它应该显示 Hello, a! 但我得到的却是:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/flavo/a
Using the URLconf defined in testDjango.urls, Django tried these URL patterns, in this order:

admin/
flavo [name='index0']
flavo a [name='a']
The current path, flavo/a, didn’t match any of these.

在testDjango/hello/views.py中

from django.http import HttpResponse
from django.shortcuts import render

def index0(request):
    return HttpResponse("Hello, world!")

def a(request):
    return HttpResponse("Hello, a!")

在testDjango/flavo/url/py中

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index0, name="index0"),
    path("a", views.a, name="a"),
]

我唯一更改的另一个文件是testDjango/testDjango/url.py“

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('flavo', include("flavo.urls")),
]

我不明白为什么我不能访问http://127.0.0.1:8000/flavo/a


Tags: pathdjangonamefrompyimporthttphello