进入 DetailView 时出现 NoReverseMatch 错误

时间:2021-01-17 19:22:34

标签: python django

每次我在我的网站上创建帖子时,当它重定向到应用程序的详细信息视图时,我总是收到 NoReverseMatch 错误。我在互联网上找不到答案。

Views.py:

from django.shortcuts import render
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy 
from . import forms
from . import models
# Create your views here.

class AnnouncementListView(generic.ListView,LoginRequiredMixin):
    model = models.Announcement

class AnnouncementDetailView(generic.DetailView, LoginRequiredMixin):
    model = models.Announcement

class AnnouncementUpdateView(generic.UpdateView, LoginRequiredMixin):
    model = models.Announcement
    form_class = forms.AnnouncementForm

class AnnouncementCreateView(generic.CreateView, LoginRequiredMixin):
    model = models.Announcement
    form_class = forms.AnnouncementForm

class AnnouncementDeleteView(generic.DeleteView, LoginRequiredMixin):
    model = models.Announcement
    success_url = reverse_lazy('announcement:single')

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(user_id=self.request.user.id)

    def delete(self, *args, **kwargs):
        messages.success(self.request, "Post Deleted")
        return super().delete(*args, **kwargs)

网址:

from django.urls import path
from . import views

app_name = 'announcement'

urlpatterns = [
    path('create/', views.AnnouncementCreateView.as_view(), name='create'),
    path('', views.AnnouncementListView.as_view(), name='list'),
    path('posts/<int:pk>/', views.AnnouncementDetailView.as_view(), name='single'),
    path('delete/<int:pk>/', views.AnnouncementDeleteView.as_view(), name='destroy'),
]

announcement_detail.html:

{% extends 'base.html' %}


{% block content %}
<div class="container">
  <h1>{{announcement.title}}</h1>
    <p>{{announcement.text}}</p>
    <span>{{announcement.date}}</span>

    <a href="{% url 'announcement:destroy' %} "><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-eraser-fill" viewBox="0 0 16 16">
        <path d="M8.086 2.207a2 2 0 0 1 2.828 0l3.879 3.879a2 2 0 0 1 0 2.828l-5.5 5.5A2 2 0 0 1 7.879 15H5.12a2 2 0 0 1-1.414-.586l-2.5-2.5a2 2 0 0 1 0-2.828l6.879-6.879zm.66 11.34L3.453 8.254 1.914 9.793a1 1 0 0 0 0 1.414l2.5 2.5a1 1 0 0 0 .707.293H7.88a1 1 0 0 0 .707-.293l.16-.16z"/>
      </svg></a>
</div>
    
{% endblock  %}

错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

需要传递公告的主键来销毁:

set.seed(42)
quarter.1 <- data.frame(id=1:3, x1=rnorm(3), x2=rnorm(3), x3=rnorm(3))
quarter.2 <- data.frame(id=1:3, x1=rnorm(3), x2=rnorm(3), x3=rnorm(3))
quarter.3 <- data.frame(id=1:3, x1=rnorm(3), x2=rnorm(3), x3=rnorm(3))
quarter.4 <- data.frame(id=1:3, x1=rnorm(3), x2=rnorm(3), x3=rnorm(3))

但是删除视图不会被 GET 请求触发,但是对于 POST 请求,这是因为 GET 请求应该创建/更新/删除实体。

因此您需要制作一个迷你表单

{% url 'announcement:destroy' announcement.pk %}

此外,mixin 应该放在视图之前,否则它们会出现在方法解析顺序 (MRO) 的末尾:

<form method="post" action="{% url 'announcement:destroy' announcement.pk %}">
    {% csrf_token %}
    <button type="submit">delete</button>
</form>

作为@NKSM says,您的class AnnouncementDeleteView(LoginRequiredMixin, generic.DeleteView): # …success_url 有同样的问题。您应该重定向到列表,或者您应该覆盖 AnnounceDeleteView,但这里重定向到已删除项的详细信息视图是没有意义的,因为它已被删除:

get_success_url