django.urls.exceptions.NoReverseMatch: 'courses' is not a registered namespace

352 views
Skip to first unread message

Bruno Gama

unread,
Apr 23, 2020, 10:46:37 AM4/23/20
to Django users
Hi, I have a problem since I tried to use get_absolute_url(self): to enter in a url beyond a photo.
When I used <a href="{{ course.get_absolute_url }}">, my index.html stopped running.

Here I have the top level url.py

    from django.contrib import admin
    from django.urls import path, include
    from simplemooc.core import views, urls
    from simplemooc.courses import views, urls
    from django.conf import settings
    from django.conf.urls.static import static
    
    
    urlpatterns = [path('admin/', admin.site.urls),
                   path('', include(('simplemooc.core.urls', 'simplemooc'), namespace='core')),
                   path('cursos', include(('simplemooc.courses.urls', 'simplemooc'), namespace='courses'))]
    
    if settings.DEBUG:
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


Here courses/url.py

    from django.urls import path
    from simplemooc.courses import views
        
    urlpatterns = [path('', views.index, name='index'),
                       path('/<slug:slug>/', views.details, name='datails')]
    
Here courses/model.py
    
    from django.db import models
    from django.urls import reverse
    
    
    class CourseManager(models.Manager):
    
        def search(self, query):
            return self.get_queryset().filter(
                models.Q(name__icontains=query) | \
                models.Q(description__icontains=query)
            )
    
    
    class Course(models.Model):
    
        name = models.CharField('Nome', max_length=100)
        slug = models.SlugField('Atalho')
        description = models.TextField('Descrição Simples', blank=True)
        about = models.TextField('Sobre o Curso', blank=True)
        start_date = models.DateField(
            'Data de Início', null=True, blank=True
        )
        image = models.ImageField(
            upload_to='courses/images', verbose_name='Imagem',
            null=True, blank=True
        )
    
        created_at = models.DateTimeField(
            'Criado em', auto_now_add=True
        )
        updated_at = models.DateTimeField('Atualizado em', auto_now=True)
    
        objects = CourseManager()
    
        def __str__(self):
            return self.name
    
        def get_absolute_url(self):
            return reverse('courses:datails', (), {'slug': self.slug})
    
        class Meta:
            verbose_name = 'Curso'
            verbose_name_plural = 'Cursos'
            ordering = ['name']

And here courses/views.py

    from django.shortcuts import render, get_object_or_404
    from django.http import HttpResponse
    
    from .models import Course
    
    def index(request):
        courses = Course.objects.all()
        template_name = 'courses\index.html'
        context = {
            'courses': courses
        }
        return render(request, template_name, context)
    
    
    def details(request, slug):
       course = get_object_or_404(Course, slug=slug)
       context = {
           'course': course
       }
       template_name = 'courses/datails.html'
       return render(request, template_name, context)

My problem happens when I use:

    <a href="{{ course.get_absolute_url }}">

in my index.html

Please, Anyone can help me?

Bruno Gama

unread,
Apr 23, 2020, 10:46:42 AM4/23/20
to Django users

I have a problem since I tried to use get_absolute_url(self): to enter in a url beyond a photo. When I used , my index.html stopped running.

Jorge Gimeno

unread,
Apr 23, 2020, 12:54:55 PM4/23/20
to django...@googlegroups.com
On Thu, Apr 23, 2020, 7:46 AM Bruno Gama <bqg...@gmail.com> wrote:

I have a problem since I tried to use get_absolute_url(self): to enter in a url beyond a photo. When I used , my index.html stopped running.

Here I have the top level url.py

from  import admin
<span class="kwd" style="font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; lin
Bruno,

I believe it may be a typo in Course.get-absolute-url.  In the reverse call, the first argument should be 'course:details".  In your code, it's 'course:datails'.

-Jorge

Bruno Gama

unread,
Apr 23, 2020, 3:05:48 PM4/23/20
to django...@googlegroups.com
I tried it, but happens the same error.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANfN%3DK9zqMGm5hxVMSQsNfS%2ByBKyuMLayv0ARK6ofuT9hg2%2BSg%40mail.gmail.com.

Raja Sekar Sambath

unread,
Apr 24, 2020, 2:34:44 AM4/24/20
to django...@googlegroups.com
decorating get_absolute_url with @staticmethod may help  I think 



--
Thanks & Regards,

Raja Sekar Sampath

Bruno Gama

unread,
Apr 24, 2020, 9:21:22 AM4/24/20
to django...@googlegroups.com
Thank you, Raja Sekar Sambath.

It stopped the error. I don't manage do what I want, but the error stopped running. 


To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAS5AE5ZD_LetM2Z3oEaXq8MyALDBMYAsxW3yDk66r%2B2PyfypQ%40mail.gmail.com.

Thanks & Regards,

Bruno Gama
+55 12 9 9171-6336. 

Raja Sekar Sambath

unread,
Apr 24, 2020, 10:50:08 AM4/24/20
to django...@googlegroups.com
Hi Bruno Gama,
Try this, update 
reverse('courses:datails', (), {'slug': self.slug}) 
as
reverse('courses:datails', kwargs={'slug': self.slug})



--
Thanks & Regards,

Raja Sekar Sambath

Bruno Gama

unread,
Apr 24, 2020, 11:27:11 AM4/24/20
to django...@googlegroups.com
Hi Raja Sekar Sambath

I tried what you said and didn't run well. I don't know why is not running with reverse function.

I managed do what I wanted. I did this:
def get_absolute_url(self):
return '%s' % self.slug

Thanks & Regards,

Bruno Gama
+55 12 9 9171-6336.

Reply all
Reply to author
Forward
0 new messages