我的项目运行良好,但是后来我决定重新制作整个内容,并且不断遇到相同的错误。我正在尝试将all_blogs.html链接到detail.html。在all_blogs.html中,我有一个标记:。每次尝试运行服务器时,都会收到消息“ /'blog'处的NoReverseMatch不是注册的名称空间”。据我了解,我们可以通过在urls.py中添加“ app_name =(namespace)”来注册名称空间。我这样做了,但是由于某种原因我仍然遇到错误。我确信它与urlpatterns中的内容有关。请查看我的代码和github链接以查看整个项目。 github预先感谢您的努力。 错误页面:
NoReverseMatch at /
'blog' is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.0.8
Exception Type: NoReverseMatch
Exception Value:
'blog' is not a registered namespace
Exception Location: C:\Python38\lib\site-packages\django\urls\base.py in reverse, line 83
Python Executable: C:\Python38\python.exe
Python Version: 3.8.5
Python Path:
['C:\\Users\\Admin\\Desktop\\webDev\\xennialsworld',
'C:\\Python38\\python38.zip',
'C:\\Python38\\DLLs',
'C:\\Python38\\lib',
'C:\\Python38',
'C:\\Users\\Admin\\AppData\\Roaming\\Python\\Python38\\site-packages',
'C:\\Python38\\lib\\site-packages']
Server time: Thu, 20 Aug 2020 22:54:36 +0000
我的urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from blog import views
app_name = 'blog'
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.all_blogs, name='all_blogs'),
path('<int:blog_id>/', views.detail, name='detail'),
]
我的all_blogs.html
{% for blog in blogs %}
<div class="row justify-content-center my-3">
<div class="col-md-10">
<a href="{% url 'blog:detail' blog.id %}">
<h2>{{ blog.title }}</h2>
</a>
<h5 class="text-muted">{{ blog.date|date:'M d Y'|upper }}</h5>
<h4>{{ Summary.description|striptags|truncatechars:100 }}</h4>
</div>
</div>
{% endfor %}
答案 0 :(得分:0)
如果您在项目中仅保留一个urls.py,则可以删除app_name ='blog'并使用<a href="{% url 'detail' blog.id %}">
调用href {p>
如果要注册名称空间:
此处的解释:https://docs.djangoproject.com/fr/3.1/topics/http/urls/#url-namespaces-and-included-urlconfs