Django 删除时不重定向到确认页面

Django don’t redirect to confirmation page when delete

Django 删除时不重定向到确认页面。

大家好,

我在 table 上做了一个标准的 CRUD,这是另一个 table 上的外键。我使用 Django 视图 class。创建、读取和更新工作,但我有一个关于删除的错误。 当我删除一个项目时,我被重定向到详细信息页面(阅读页面),其中包含正确的删除地址('localhost/table_name/pk/delete')。
我遵循 Django 项目和几个教程中描述的标准方法。

以下是我的部分代码:

model.py :

class Badge(models.Model):
    id_badge = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100)
    category = models.CharField(max_length=60)
    level = models.IntegerField()
    image_badge = models.ImageField(upload_to='badge_image/', null=True, blank=True, default='ex-par-nat_logo.png')

    class Meta:
        managed = True
        db_table = 'badge'

view.py:

from django.shortcuts import redirect, render
from UsersApp.tribut_sign_up_form import TributSignUpForm
from UsersApp.tutor_sign_up_form import TutorSignUpForm
from UsersApp.child_sign_up_form import ChildSignUpForm
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout as account_logout
from UsersApp.models import Tribut, Tutor, Child, Account
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from UsersApp.decorator import tribut_required

@method_decorator([login_required, author_required], name='dispatch')
class BadgeListView(ListView):
    model = Badge
    ordering = ('title', )
    template_name = 'ArticlesApp/badge_list.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

    def get_queryset(self):
        queryset = Badge.objects.all()
        return queryset

@method_decorator([login_required, author_required], name='dispatch')
class BadgeCreateView(CreateView):
    model = Badge
    # form_class = ArticleCreationForm
    fields = ('title', 'category', 'level', 'image_badge')
    template_name = 'ArticlesApp/badge_create.html'

    def get_context_data(self, **kwargs):
        return super().get_context_data(**kwargs)

    def form_valid(self, form):
        form.save()
        return redirect('badge_list')

@method_decorator([login_required], name='dispatch')
class BadgeReadView(DetailView):
    model = Badge
    template_name = 'ArticlesApp/badge_read.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

@method_decorator([login_required, author_required], name='dispatch')
class BadgeUpdateView(UpdateView):
    model = Badge
    fields = ['title', 'category', 'level', 'image_badge']
    template_name = 'ArticlesApp/badge_update.html'

    def get_success_url(self):
        view_name = 'badge_list'
        return reverse_lazy(view_name)

@method_decorator([login_required, author_required], name='dispatch')
class BadgeDeleteView(DeleteView):
    model = Badge
    success_url = reverse_lazy('badge_list')

url.py:

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
from ArticlesApp import views as article_views

urlpatterns = [
    path('badge/', article_views.BadgeListView.as_view(), name='badge_list'),
    path('badge/add/', article_views.BadgeCreateView.as_view(), name='badge_create'),
    path('badge/<int:pk>/<slug:slug>/', article_views.BadgeReadView.as_view(), name='badge_read'),
    path('badge/<int:pk>/', article_views.BadgeUpdateView.as_view(), name='badge_update'),
    path('badge/<int:pk>/delete/',
         article_views.BadgeDeleteView.as_view(
             template_name='ArticlesApp/badge_confirm_delete.html'),
         name='badge_confirm_delete'),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

badge_confirm_delete.html:

{% extends "base_user.html" %}
{% block content %}
<form method="post">{% csrf_token %}
    <p>Are you sure you want to delete "{{ object }}"?</p>
    <input type="submit" value="Confirm">
</form>
{% endblock %}

非常感谢您的帮助, 塞巴斯蒂安

Django 按照定义的顺序遍历 url 模式,returns 第一个匹配传入路径的模式。

路径“badge/1/delete/”匹配“badge_read”url,因为它是第一个匹配的 url(“删除”是一个有效的 slug)。

将“badge_confirm_delete”url 移到“badge_read”url 之前,以便它首先匹配

urlpatterns = [
    path('badge/', article_views.BadgeListView.as_view(), name='badge_list'),
    path('badge/add/', article_views.BadgeCreateView.as_view(), name='badge_create'),
    path('badge/<int:pk>/delete/',
         article_views.BadgeDeleteView.as_view(
             template_name='ArticlesApp/badge_confirm_delete.html'),
         name='badge_confirm_delete'),
    path('badge/<int:pk>/<slug:slug>/', article_views.BadgeReadView.as_view(), name='badge_read'),
    path('badge/<int:pk>/', article_views.BadgeUpdateView.as_view(), name='badge_update'),
]

现在这意味着“删除”不能用作 slug,如果这是一个问题,您可以重新排列路径的各个部分,以便首先执行操作,例如“badge/delete/int:pk /