django 使用slug后无反向匹配

tct7dpnv  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(139)

我一直试图在Django项目中采用slugs来给予漂亮的URL,因为我在Django项目中有不止一个应用程序,但我遇到了这个错误。我被困住了,我不知道该怎么解决。
将引发以下错误

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

字符串
以下是appname/models.py代码

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'),  
]


一切都工作得很好,直到我添加了最后一条路径,那就是所有东西都从屋顶出来的地方。
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"


模板码

{% 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 %}


我错过了什么?

62lalag4

62lalag41#

url标签中的字段与课程模型不匹配:这是教训。蛞蝓(尾随s!)和课程标准(非标准)

相关问题