如何修复 Django 中找不到的页面?

问题描述 投票:0回答:0

我已经创建了一个菜单显示模板,但是当我运行我的服务器时,我得到一个错误,说找不到我的页面。有什么建议吗?

from django.urls 导入路径 从餐厅导入视图 app_name = '餐厅'

urlpatterns = [
    path('', views.index, name='index'),
    path('about/', views.about, name = 'about'),
    path('menu/<slug:menu_name_slug>/', views.show_menu, name='show_menu'),
    path('restricted/', views.restricted, name='restricted'),
]
Error Message: Using the URLconf defined in WAD_Group_Project.urls, Django tried these URL patterns, in this order:
[name='index']
restaurant/ [name='index']
restaurant/ about/ [name='about']
restaurant/ restaurant/menu/<slug:menu_name_slug>/ [name='show_menu']
restaurant/ restricted/ [name='restricted']
admin/
accounts/
^media/(?P<path>.*)$
The current path, restaurant/menu/, didn't match any of these.


def show_menu(request, menu_name_slug):
    context_dict = {}
    
    try:
        menu = Menu.objects.get(slug=menu_name_slug)
        menuItems = MenuItem.objects.filter(menu=menu)
        context_dict['menuItems'] = menuItems
    except Menu.DoesNotExist:
        context_dict['menuItems'] = None

    return render(request, 'menu.html', context=context_dict)
django django-templates django-urls
© www.soinside.com 2019 - 2024. All rights reserved.