NoReverseMatch用于基于类的删除视图

时间:2019-06-26 08:46:06

标签: django

由于遇到了NoReverseMatch错误,我无法尝试实现基于类的删除视图,而且我不确定导致问题的原因。

当我尝试访问deleteview时,这是错误: / post / lesson_delete / 3处的NoReverseMatch 找不到带有参数“('',)”的“ lesson_uploaded”。尝试了1个模式:['post \ /(?P [0-9] +)\ / lesson_uploaded \ / $']

这是我的urls.py:

urlpatterns = [
path('', PostListView.as_view(), name='store-home'),
path('sub_home', SubListView.as_view(), name='store-sub_home'),
path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),
path('about/', views.about, name='store-about'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:post_id>/lesson_upload/', UploadLessonView.as_view(), name='lesson_upload'),

path('post/<int:post_id>/lesson_uploaded/', LessonListView.as_view(), name='lesson_uploaded'),
path('post/lesson_delete/<int:pk>', LessonDeleteView.as_view(), name='lesson_delete'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),

path('post/<int:pk>/change_sub/<slug:operation>/', views.change_sub, name='change_sub'),

path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('register/', user_views.register, name='register'),
path('profile/', user_views.profile, name='profile'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),   
path('password-reset/',
 auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'), name='password_reset'),   
path('password-reset/done/',
 auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'), name='password_reset_done'),   
path('password-reset/confirmed/<uidb64>/<token>',
 auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'), name='password_reset_confirm'),   
path('password-reset-complete/',
 auth_views.PasswordResetView.as_view(template_name='users/password_reset_complete.html'), name='password_reset_complete'),   
path('post/new/', PostCreateView.as_view(), name='post-create'),

] 

这是我的DeleteView的views.py:

class LessonDeleteView(LoginRequiredMixin, UserPassesTestMixin, 
DeleteView):
    model = Lesson
    success_url = '../'

    def test_func(self):
        lesson = self.get_object()
        # if self.post.request.user == lesson.post.author:
        return True
        # return False

这些是我的模特:

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

    def delete(self, *args, **kwargs):
        self.file.delete()
        self.title.delete()
        super().delete(*args, **kwargs)

class Post(models.Model):
    title = models.CharField(max_length=100)
    image = models.ImageField(default = 'default0.jpg', 
upload_to='course_image/')
    description = models.TextField()
    price = models.DecimalField(decimal_places=2, max_digits=6)
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    rating = models.IntegerField(default = 0)

    def __str__(self):
        return self.title

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

这是我的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' l.id %}">Delete</a>
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>
{% endblock %}

我无法显示我的deleteview,也不确定是什么问题。我的猜测是在我的html模板中,我没有为lesson.id正确编写。

0 个答案:

没有答案