制作新 url / 查看 Django 时找不到页面

page not found when making new url / view Django

您好,我正在尝试添加一个名为 localhost:8000/shop

的基本 url

这样当我在我的主页上时,我可以点击一个名为 shop 的 link,它将引导我到 localhost:8000/shop

在我的 urls.py 我添加了

from django.conf import settings
from django.conf.urls.static import static

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




urlpatterns = [
    path('' , views.home),
    path('admin/', admin.site.urls),
    path('reviews/' , include('reviews.urls')),
    path('shop/' , include('product.urls')),

] 

在我名为 product 的文件夹中,我有一个 urls.py 文件

from django.urls import include, path
from . import views

urlpatterns = [
    path('shop/' , views.shop),   
        
]

在我的产品文件夹中,我有一个 views.py 文件

from django.shortcuts import render


# Create your views here.

def shop(request):
    return render(request, 'product/shop.html')

link将其添加到我的产品文件夹中的 html 文件中.. 当我 运行 服务器时,我收到此错误消息

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

admin/
reviews/
shop/
The current path, shop, didn't match any of these.

我做错了什么?!

由于 path('shop/', include('product.urls'))product 应用程序中的所有 url 模式已经以 shop/ 开头。因此,products 应用程序的 urls.py 应如下所示:

# product/urls.py

from django.urls import include, path
from . import views

urlpatterns = [
    path(<b>''</b> , views.shop),   
]

否则路径应该是/shop/shop/.

要注册路径 /shop,您需要在您的商店应用程序的 urls.py 中使用 path('' , views.shop),/shop 前缀已由项目级别 urls.py 中的 path('shop/' , include('product.urls')), 行定义。