You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置DeleteView时出现路由模式错误,请求排查slug相关问题

Hey there! Let's work through this Lecture DeleteView routing issue you're hitting with your Course slugs. From what I've seen with nested Django resources like this, here's how to diagnose and fix it:

1. Double-Check Your URL Configuration Includes Required Params

Your DeleteView needs to capture both the course slug and a unique identifier for the lecture (like pk or its own slug) since the delete action is tied to a lecture under a specific course. Here's how a correct route should look:

# urls.py
from django.urls import path
from .views import LectureDeleteView

urlpatterns = [
    # ... your other course/lecture routes
    path('courses/<slug:course_slug>/lectures/<int:pk>/delete/', LectureDeleteView.as_view(), name='lecture-delete'),
]

Make sure the parameter names here match what your view expects (e.g., course_slug should be consistent everywhere).

2. Configure the DeleteView to Tie to the Correct Course

You need to restrict the DeleteView to only delete lectures belonging to the specified course, and set up the success redirect back to the course's detail page. Here's a sample view implementation:

# views.py
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
from .models import Course, Lecture

class LectureDeleteView(DeleteView):
    model = Lecture
    template_name = 'lectures/lecture_confirm_delete.html'

    def get_queryset(self):
        # Ensure users can only delete lectures from the specified course
        course = Course.objects.get(slug=self.kwargs['course_slug'])
        return self.model.objects.filter(course=course)

    def get_success_url(self):
        # Redirect back to the course detail page after deletion
        return reverse_lazy('course-detail', kwargs={'slug': self.kwargs['course_slug']})

The key here is using self.kwargs to pull the course_slug from the URL, filter the lecture queryset, and build the correct redirect path.

On the course detail page (where you list all lectures), your delete link must pass both the course_slug and the lecture's pk to match the URL pattern. Example template code:

<!-- course_detail.html -->
{% for lecture in course.lectures.all %}
    <div class="lecture-entry">
        <h3>{{ lecture.title }}</h3>
        <a href="{% url 'lecture-delete' course_slug=course.slug pk=lecture.pk %}">Delete This Lecture</a>
    </div>
{% endfor %}

A common mistake here is misnaming parameters (like using slug instead of course_slug), which will trigger a routing error immediately.

4. Verify Your Model Relationships

Make sure your Lecture model has a proper foreign key to Course, with a reverse relationship set up for easy access:

# models.py
from django.db import models
from django.utils.text import slugify

class Course(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

class Lecture(models.Model):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='lectures')
    title = models.CharField(max_length=200)
    # Add your other lecture fields here

The related_name='lectures' lets you use course.lectures.all to fetch all lectures for a course, which is handy in both templates and views.

If you're still hitting errors, look closely at the specific message in your traceback—things like "Reverse for 'lecture-delete' with no arguments not found" or "Keyword argument 'course_slug' not provided" will point exactly to where the mismatch is happening.

内容的提问来源于stack exchange,提问作者user9092892

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:12:46