NoReverseMatch at / blog / Django 2.0.5

时间:2018-06-07 14:37:20

标签: python django

我正在关注Django教程。由于Django版本不同,我遇到了这个问题,我无法弄明白。提前致谢

NoReverseMatch at /blog/
'blog' is not a registered namespace
Request Method: GET
Request URL:    http://127.0.0.1:8000/blog/
Django Version: 2.0.5
Exception Type: NoReverseMatch
Exception Value:    
'blog' is not a registered namespace
Exception Location: /Users/sumeixu/anaconda3/lib/python3.6/site-packages/django/urls/base.py in reverse, line 84
Python Executable:  /Users/sumeixu/anaconda3/bin/python
Python Version: 3.6.3
Python Path:    
['/Users/sumeixu/djangotest',
 '/Users/sumeixu/anaconda3/lib/python36.zip',
 '/Users/sumeixu/anaconda3/lib/python3.6',
 '/Users/sumeixu/anaconda3/lib/python3.6/lib-dynload',
 '/Users/sumeixu/anaconda3/lib/python3.6/site-packages',
 '/Users/sumeixu/anaconda3/lib/python3.6/site-packages/aeosa']
Server time:    Thu, 7 Jun 2018 14:32:43 +0000

博客/ urls.py:

from django.conf.urls import url
from django.urls import path
from . import views

urlpatterns =[
    path('', views.list_of_post,name='list_of_post'),
    path('<slug:slug>/', views.list_of_post,name='post_detail')
]

urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls'),name='blog'),
]

views.py

def post_detail(request,slug):
    post = get_object_or_404(Post,slug=slug)
    template = 'blog/post/post_detail.html'
    return render(request,template,{'post':post})

1 个答案:

答案 0 :(得分:4)

Key |Value ------------------------------------- SELECT |poc, event, network, vendor FROM |bLine WHERE |network = 'something' AND |event = 'simple' OR |event != 'hard' ORDER BY |poc ?? |ASC

中删除namespace / name
include

然后在urlpatterns = [ path('admin/', admin.site.urls), path('blog/', include('blog.urls')), ] 中设置app_name

blog/urls.py

您可以在Django 1.9 release notes中详细了解此更改。从Django 2.0开始,除非设置了app_name = 'blog' urlpatterns =[ path('', views.list_of_post,name='list_of_post'), path('<slug:slug>/', views.list_of_post,name='post_detail') ] ,否则无法在namespace中设置include