【问题标题】:django.urls.exceptions.NoReverseMatch: Reverse for 'home' not found. 'home' is not a valid view function or pattern namedjango.urls.exceptions.NoReverseMatch:找不到“家”的反向。 'home' 不是有效的视图函数或模式名称
【发布时间】:2020-08-02 15:35:04
【问题描述】:

我有一个问题,我的网址被视为无效网址。我的所有 URL 都不适用于我的 Django 应用程序。我犯了使用与另一个应用程序相同的密钥的错误。这是我的错误消息、url 页面和我的视图的图片。

Error Message

urls.py

views.py

from django.shortcuts import render
from home.models import Products

#This is the store view
def home(request):
    return render(request,'home.html')

#This is the About Us page view
def AboutUs(request):
    return render(request,'AboutUs.html')

#This is the Long Arm Services View
def LongArmServices(request):
    return render(request,'LongArmServices.html')

#This is the product View
def product(request):
    return render(request,'product.html')

urls.py:

from django.urls import path
from . import views
from django.http import HttpResponse


app_name='home'

urlpatterns = [
    path('',views.home,name='home'),
    path('about_us/',views.AboutUs,name='AboutUs'),        path('long_arm_services/',views.LongArmServices,name='LongArmServices'),
    path('product/',views.product,name='product'),
]

【问题讨论】:

  • 请包含 code,而不是 images 的代码:idownvotedbecau.se/imageofcode
  • views.py: from django.shortcuts import render from home.models import Products #这是商店视图 def home(request): return render(request,'home.html') #这是关于我们页面视图 def AboutUs(request): return render(request,'AboutUs.html') #这是长臂服务视图 def LongArmServices(request): return render(request,'LongArmServices.html') #这是产品视图 def product(request): return render(request,'product.html')
  • edit提问。问题是模板和urls.py 的组合。见答案。

标签: python-3.x django


【解决方案1】:

您的urls.py 指定app_name = 'home',这意味着您需要在视图名称前加上app_name 和冒号(:)。所以你应该将模板中的标记部分重写为:

href="{% url '<b>home:</b>home' %}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2021-05-18
    • 2018-03-29
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    相关资源
    最近更新 更多