为什么基于类的删除视图不起作用?

时间:2019-06-26 18:31:12

标签: django

我的删除视图不起作用,错误消息是: 找不到页面(404)请求方法:GET。

我正在尝试根据主键删除上传的文件,到目前为止,我的URL能够根据pk正确链接我。

这是我的urls.py:

path('post/<int:post_id>/lesson_delete/<int:lesson_id>', LessonDeleteView.as_view(), name='lesson_delete'),

我的views.py:

class LessonDeleteView(DeleteView):
model = Lesson
success_url = '../'
template_name = 'lesson_confirm_delete.html'

这是将用户带到删除视图的html模板:

{% extends "store/base.html" %}
{% block content %}
<div id="main">
    <table class="table mb-0">
    <thead>
      <tr>          
        <th>Title</th>
        <th>Author</th>
        <th>Download</th>
        <th>Delete</th>
      </tr>
    </thead>
    <tbody>
      {% for l in Lesson %}
      <tr>
        <td>
            {% if l.file %}
                {{ l.title }}
            {% else %}                  
            <h6>Not available</h6>
            {% endif %}
        </td>
        <td>{{ l.post.author }}</td>
        <td>{% if l.file %}                 
            <a href="{{ l.file.url }}" class="btn btn-primary btn-sm" target="_blank">Download</a>
            {% else %}
            <h6>Not available</h6>
            {% endif %}    
        </td>
        <td> <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'lesson_delete' lesson_id=l.id %}">Delete</a>
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>
{% endblock %}

这是我的DeleteView的html模板:

{% extends "store/base.html" %}
{% block content %}
<div class="content-section" id="main">
<form method="POST">
    {% csrf_token %}
    <fieldset class="form-group">
        <legend class="border-bottom mb-4">Delete Lesson</legend>  
        <h2>Are you sure you want to delete the post "{{object.title}}"? 
</h2>              
    </fieldset>
    <span style="display:inline;">
            <button class="btn btn-outline-danger" type="submit">Yes, Delete! 
    </button>
        <a class="btn btn-outline-secondary" href ="">Cancel</a>
    </span>
</form>        
</div>
    {% endblock content %}

这是我的课程模型:

class Lesson(models.Model):
    title = models.CharField(max_length=100)
    file = models.FileField(upload_to="lesson/pdf")
    date_posted = models.DateTimeField(default=timezone.now)
    post = models.ForeignKey(Post, on_delete=models.CASCADE, null=False, blank=False)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('lesson_upload', kwargs={'pk': self.pk})

1 个答案:

答案 0 :(得分:0)

您是否将模板称为“ lesson_confirm_delete.html”? 另外,对于您的成功网址,我感到您没有路径“ ../”。它应该是您要转到的特定路径。

(对不起,我无法发表评论。)