我是Django的新手。我有一个项目有2个儿童应用程序:"访问"和"主要"。该错误位于导航栏的href的base.html文件中。 我正面临无反向匹配发现错误。
主/ base.html文件:
{%load staticfiles%}
<link href="{%static "main/vendor/bootstrap/css/bootstrap.min.css"%}" rel="stylesheet">
<link href="{%static "main/css/small-business.css"%}" rel="stylesheet">
</head>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="/home.html">Ekyam</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href='{%url 'main:home'%}'>Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href='{%url 'main: incubators'%}'>Incubators</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
{%block content%}
{%endblock%}
这是我面临的错误
NoReverseMatch at /home.html
Reverse for ' incubators' not found. ' incubators' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/home.html
Django Version: 1.11.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for ' incubators' not found. ' incubators' is not a valid view function or pattern name.
Exception Location: C:\Users\my dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable: C:\Users\my dell\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.1
Python Path:
['C:\\Users\\my dell\\Desktop\\Projects\\junityme'
帮助将不胜感激。 网站网址:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('visit.urls')),
url(r'^', include('main.urls', namespace='main')),
]
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.home, name='home'),
url(r'/incubators/', views.incubators, name='incubators'),
url(r'/about/', views.about, name='about'),
]
我已发布了url文件。正如您所看到的,我已经为&#34; main&#34;声明了名称空间。用于在base.html中引用它的应用程序。 我在&#34;访问&#34; app我设置了注册/登录门户网站,将用户重定向到&#34; main&#34;应用程序。登录将解锁应用程序的主要功能。
views.py:
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'main/home.html')
def incubators(request):
return render(request, 'main/incubators.html')
def about(request):
return HttpResponse("dsfsddsf")
答案 0 :(得分:0)
您的网址存在一些问题。进行这些更改不会解决您的具体错误,但无论如何都应该这样做。
在您的主urls.py中,您应该更新导入,并删除命名空间:
from django.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('visit.urls')),
url(r'^', include('main.urls')),
]
在main/urls.py
中,添加app_name = 'main'
,因为您已从include()
中删除了命名空间。您的正则表达式应以^
开头,并以$
结尾(不要包含$
include()
)。正则表达式在开头不应该有斜杠。
app_name = 'main'
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^incubators/$', views.incubators, name='incubators'),
url(r'^about/$', views.about, name='about'),
]
答案 1 :(得分:0)
错误报告:
Reverse for ' incubators' not found.
之前有一个空格&#39;孵化器&#39;否则它会说'孵化器'。博鲁特已经说明了这一点。
要解决此问题,您必须使用:
'{%url 'main:incubators'%}'
而不是
'{%url 'main: incubators'%}'
我只删除了孵化器前面的空间。你好像做了同样的事,但仍然得到了错误。在这种情况下,您是否在“home.html&#39;”中多次使用此行?因为如果你这样做了,你必须改变所有的实例。
如果你正确地更改了上面的代码但仍然得到了同样的错误,就像你说的那样,错误现在应该看起来有点不同了。现在应该说:
Reverse for 'incubators' not found. 'incubators' is not a valid view function or pattern name.
请注意,它现在不应该在孵化器前面有初始空间。
试着在这里彻底。