gpt4 book ai didi

python - 我无法让 Django DeleteView 工作

转载 作者:行者123 更新时间:2023-12-01 02:06:48 25 4
gpt4 key购买 nike

因此,我尝试为我的 Lecture 模型设置一个 DeleteView。我的 Course 模型中的每门类(class)都有自己的 slug 页面。该页面上会出现类(class)讲座。

问题是我尝试为讲座添加删除按钮,但没有成功。

这是我所做的:

  #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)

问题是这样的: 如果在 course.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')

最佳答案

您的模型中没有任何作为 slug 的字段。

你只需要做:

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

href

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

关于python - 我无法让 Django DeleteView 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48975787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com