我不能让Django DeleteView工作

时间:2018-02-25 16:30:22

标签: python django django-views

所以我试图为DeleteView模型设置Lecture。我的Course模型中的每个课程都有自己的slug页面。在那个页面上出现课程讲座。

我试图为讲座添加一个删除按钮,但是没有成功。

以下是我的所作所为:

  #courses.html
  
  {% regroup lectures by get_lecture_category_display as category_list %}
        <h3>Lectures</h3>
        <ul>
            {% for category in category_list %}
                <strong>
                    <li>{{ category.grouper }}</li>
                </strong>
                <ul>
                    {% for c in category.list %}
                        .............
                        <li>{{ c.lecture_title }}</li>
                        <li>{{ c.content }}</li>
                        {% for file in c.files.all %}
                            {% if file.files %}
                                <li><a href='{{ MEDIA_URL }}{{ file.files.url }}'>download</a></li>
                            {% endif %}
                        {% endfor %}
                       <a href="{% url 'courses:lecture_delete' c.slug %}">Delete</a>
                    {% endfor %}
                </ul>
            {% endfor %}
        </ul>

#lecture_confirm_delete.html

<form method="post">
    {% csrf_token %}
    Are you sure you want to delete?
    <input type="submit" value="Confirm"/>
</form>

#urls.py

    path('<slug:slug>/', views.courses, name='courses'),
    path('<slug:slug>/delete/', views.LectureDelete.as_view(), name='lecture_delete'),


#models.py

class Lecture(models.Model):
    LECTURE_CHOICES = (
        ('Courses', 'Courses'),
        ('Seminars', 'Seminars'),
    )
    course = models.ForeignKey('Course', on_delete=models.CASCADE, default='', related_name='lectures', )
    lecture_category = models.CharField(max_length=10, choices=LECTURE_CHOICES, default='Courses', )
    lecture_title = models.CharField(max_length=100, blank=True, null=True)
    content = models.TextField(blank=False, default=No
 
 
class Course(models.Model):
    study_programme = models.ForeignKey('StudyProgramme', on_delete=models.CASCADE, default='')
    name = models.CharField(max_length=50, unique=True)
    ects = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
    description = models.TextField()
    year = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
    semester = models.IntegerField(choices=((1, "1"),
                                            (2, "2"),
                                            ), default=None)
    teacher1 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None,
                                 verbose_name="Course Teacher", related_name='%(class)s_course_teacher')
    teacher2 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None, null=True,
                                 verbose_name="Seminar Teacher", related_name='%(class)s_seminar_teacher')
    slug = models.SlugField(max_length=140, unique=True)
 
 

问题是这样的:  如果在courses.html链接中我放了c.slug,那么我收到此错误:http://dpaste.com/1K1T8N8  但是,如果我添加c.idc.pk,则会显示页面,如果我按下删除按钮,则会出现此错误:http://dpaste.com/3TRSJE2

以下是我的观点:

class LectureDelete(DeleteView):
    model = Lecture
    success_url = reverse_lazy('courses:index')

1 个答案:

答案 0 :(得分:2)

模型中没有slug字段。

你只需要这样做:

path('<int:id>/delete/', views.LectureDelete.as_view(), name='lecture_delete'),

并在href

href="{% url 'courses:lecture_delete' c.id %}"