无法配置DeleteView,导致模式错误

时间:2018-02-25 08:53:38

标签: python django

所以我一直在尝试为我的Lecture模型配置DeleteView。对于我的课程模型,每个课程都有自己的slug页面,在那个页面上,我有所有课程的讲座。问题是,当我试图删除讲座时,我收到了这个错误:



Reverse for 'lecture_delete' with no arguments not found. 1 pattern(s) tried: ['courses\\/(?P<slug>[-a-zA-Z0-9_]+)\\/delete\\/$']
&#13;
&#13;
&#13;

我想这与我的slug有关。追溯:http://dpaste.com/02X45B5

&#13;
&#13;
class LectureDelete(DeleteView):
    model = Lecture
    success_url = reverse_lazy('courses/courses.html')


 <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 %}
                        Hey, are you sure you want to delete {{ c.lecture_title }}?
                        <form action="{% url "courses:lecture_delete" %}" method="post">
                            {% csrf_token %}
                            <button  type="submit">Yeap, I'm sure.</button>
                        </form>
                    {% endfor %}
                </ul>
&#13;
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=None)

    def __str__(self):
        return str(self.lecture_title)


class FileUpload(models.Model):
    files = models.FileField(upload_to='documents', null=True, blank=True)
    lecture = models.ForeignKey('Lecture', related_name='files', on_delete=None, default=None)
&#13;
 path('<slug:slug>/', views.courses, name='courses'),
 path('<slug:slug>/delete/', views.LectureDelete.as_view(), name='lecture_delete'),
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我认为您的问题在于在模板中编写您的网址。

<a href="{% url 'delview:item-deleteview' object.slug %}">Delete</a>

以上代码是用于创建删除视图的有效代码。请注意,我在url模板标记中写了object.slug,以便为Django提供slug以创建正确的url。我认为这是你的问题。

以下是我为测试我的DeleteView而进行的演示项目的详细信息。

<强> urls.py

urlpatterns = [
    path('item/<slug:slug>/', views.ItemDetail.as_view(), name='item-detailview'),
    path('item/<slug:slug>/delete/', views.ItemDelete.as_view(), name='item-deleteview'),
]

<强> views.py

class ItemDetail(generic.DetailView):
    model = Item
    template_name = 'delview/detail.html'


class ItemDelete(generic.DeleteView):
    model = Item
    success_url = '/admin/'

我无缘无故地使用/admin/作为我的成功网址。

<强> detail.html

{{ object.name }} <br/>

{{ object.slug }} <br/>

<a href="{% url 'delview:item-deleteview' object.slug %}">Delete</a>

请查看我在上面创建的网址。如果有人点击此链接,他将被带到删除视图。我已删除object.slug变量并成功重新创建了您的错误消息。所以我认为你可以在这里找到你的解决方案。

<强> item_confirm_delete.html

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

请注意,这是确认删除页面,我没有为表单元素提供任何操作属性。

请提供以下评论部分的输出反馈。