Sue-May Xu
Sue-May Xu

Reputation: 500

NoReverseMatch at /blog/ Django 2.0.5

I am following a Django tutorial. Because of different Django version, I had this problem and I could not figure it out. Thanks in advance

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

blog/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})

Upvotes: 0

Views: 126

Answers (1)

Alasdair
Alasdair

Reputation: 308839

Remove namespace/name from the include:

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

Then set app_name in blog/urls.py:

app_name = 'blog'

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

You can read more about this change in the Django 1.9 release notes. As of Django 2.0, you can't set namespace in the include unless app_name is set.

Upvotes: 4

Related Questions