Search code examples
djangodjango-urls

No Reverse match after using slug


I have been trying to adopt slugs in Django project to give beautiful urls since i have more than one app in the Django project but I have encountered this error. Am stuck and I do not know to sort it out.

The following errors is thrown

Reverse for 'lesson_detail' with keyword arguments '{'slug': '', 'subject': '001', 'standard': ''}' not found. 1 pattern(s) tried: ['curriculum(?P<standard>[^/]+)/(?P<subject>[^/]+)/(?P<slug>[-a-zA-Z0-9_]+)/\\Z']
Request Method: GET
Request URL:    http://127.0.0.1:8000/curriculumprimary-four/001/
Django Version: 3.2.12
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'lesson_detail' with keyword arguments '{'slug': '', 'subject': '001', 'standard': ''}' not found. 1 pattern(s) tried: ['curriculum(?P<standard>[^/]+)/(?P<subject>[^/]+)/(?P<slug>[-a-zA-Z0-9_]+)/\\Z']
Exception Location: /usr/lib/python3/dist-packages/django/urls/resolvers.py, line 698, in _reverse_with_prefix
Python Executable:  /usr/bin/python3
Python Version: 3.10.6
Python Path:    
['/home/aiden/Projects/LMS/LMS',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/aiden/.local/lib/python3.10/site-packages',
 '/usr/local/lib/python3.10/dist-packages',
 '/usr/lib/python3/dist-packages']
Server time:    Mon, 24 Jul 2023 17:08:45 +0000

The following are appname/models.py code

class Standard(models.Model):
    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(null=True, blank = True)
    description = models.TextField(max_length=500, blank=True)

    def __str__(self):
        return self.name
    
    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        return super().save(*args, **kwargs)   

class Subject (models.Model):
    subject_id = models.CharField(max_length=100, unique= True)
    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(null=True, blank=True)
    standard = models.ForeignKey(Standard,on_delete=models.CASCADE,related_name="subjects")
    image = models.ImageField(upload_to=save_subject_img, blank=True,verbose_name='Subject Image')
    description = models.TextField(max_length=500, blank=True)

    def __str__(self):
        return self.name
    
    def save(self, *args, **kwargs):
        self.slug = slugify(self.subject_id)
        return super().save(*args, **kwargs)


class Lesson (models.Model):
    lesson_id = models.CharField(max_length=250, unique=True)
    standard = models.ForeignKey(Standard, on_delete=models.CASCADE)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE, related_name="lessons")
    name = models.CharField(max_length=250)
    position = models.PositiveSmallIntegerField(verbose_name="chapter no.")
    slugs = models.SlugField(null=True, blank=True)
   

    class Meta:
        ordering = ['position']

    def __str__(self):
        return self.name
    
    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        return super().save(*args, **kwargs)
 

appname\urls.py


app_name = 'appname'
urlpatterns = [
    path('',views.StandardListView.as_view(),name='standard_list'),
    path('<slug:slug>', views.SubjectListView.as_view(), name='subject_list'),
    path ('<str:standard>/<slug:slug>/', views.LessonListView.as_view(),name='lesson_list'),
  path ('<str:standard>/<str:subject>/<slug:slug>/', views.LessonDetailView.as_view(),name='lesson_detail'),  
]

Everything was working just fine until i added the last path, that is where everything went out of the roof.

views.py

from django.shortcuts import render
from django.views.generic import (TemplateView,DetailView,ListView,FormView)
from .models import*


class StandardListView(ListView):
    context_object_name ='standards'
    model = Standard
    template_name = "appname/standard_list_view.html"


class SubjectListView(DetailView):
    context_object_name ='standards'
    model = Standard
    template_name = "appname/subject_list_view.html"


class LessonListView(DetailView):
    context_object_name ='subjects'
    model = Subject
    template_name = "appname/lesson_list_view.html"


class LessonDetailView(DetailView):
    context_object_name ='lessons'
    model = Lesson
    template_name = "appname/lesson_detail_view.html"

template code

{% extends 'base.html' %}
{% block content %}

{% for lesson in subjects.lessons.all  %}
<a href="{% url 'appname:lesson_detail' slug=lesson.slug subject=subjects.slug standard=lesson.Standard.slug %}">Chapter--{{lesson.position}}: {{lesson.name}}</a>
{% endfor %}

{% endblock content %}

What am I missing?


Solution

  • the fields in the url tag are not matching the lessons model: it is lesson.slugs (trailing s!) and lesson.standard (not Standard)