NoReverseMatch at /'blog' is not a registered namespace

Viewed 2684

I am trying to link two pages. Page files are home.html and pageOne.html. I am getting "NoReverseMatch at /'blog' is not a registered namespace". I am using django. When I first created the app, I named it artclBlog, I then created a templates folder and another folder within that one, this one I named blog. I think I should have kept these two names the same, this may have caused some confusion in my code.

pic of error

my views.py

from django.shortcuts import render, get_object_or_404
from .models import Blog

def home(request):
    blogs = Blog.objects.order_by('-date')
    return render(request, 'blog/home.html', {'blogs': blogs})

my 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 artclBlog import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('<int:blog_id>/', views.PageOne, name='PageOne')
            ]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

my models.py

from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=200, default='')
    summary = models.CharField(max_length=200, default='')
    pageOne = models.TextField(default='')
    pageTwo = models.TextField(default='')
    pageThree = models.TextField(default='')
    pageFour = models.TextField(default='')
    date = models.DateField(default='')
   
    def __str__(self):
        return self.title

home.html

{% for blog in blogs %}
<a href="{% url 'blog:PageOne' blog.id %}">
 <h2 id="txt">{{ blog.title }}</h2>
</a>
    <h4 id="txt">{{ blog.date|date:'M d y' }}</h4>

    <p id="txt">{{ blog.summary|truncatechars:190 }}</p>

    <hr>
{% endfor %}
1 Answers

You did not define an app_name in your urls.py, hence that means that blog: in blog:PageOne makes no sense. You thus either define an app_name, or remove the namespace.

Option 1: Add an app_name in urls.py

You can specify the namespace by writin an app_name in the 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 artclBlog import views

app_name = 'blog'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('<int:blog_id>/', views.PageOne, name='PageOne')
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This however means that thus all views now use this namespace, including home for example.

Option 2: Remove the namespace

Another option is to remove the namespace in the {% url … %} template tag [Django-doc]:

{% for blog in blogs %}
<a href="{% url 'PageOne' blog.id %}">
 <h2 id="txt">{{ blog.title }}</h2>
</a>
    <h4 id="txt">{{ blog.date|date:'M d y' }}</h4>

    <p id="txt">{{ blog.summary|truncatechars:190 }}</p>

    <hr>
{% endfor %}
Related