Django_tables2 带有删除按钮

Django_tables2 with delete buttom

最近怎么样?我正在学习在 Django 中编程。目前我正在构建一个简单的应用程序,它利用表单更新引用的 table.

现在我尝试在我的 table 的每一行中添加一个删除按钮,但是,除了我已经尝试了很多解决方案之外,我没有找到一个可以正常工作的。

在我的代码下方:

网址


from django.urls import path
from app import views

app_name = 'main'

urlpatterns = [
    path('', views.homepage, name='homepage'),
    path('delete_item/<int:pk>', views.delete_item, name="delete_item"),
]

表格

from django import forms
from .models import Income

class IncomeModelForm(forms.ModelForm):

    class Meta:
        model = Income
        fields = "__all__"

tables

import django_tables2 as tables
from django_tables2.utils import A
from .models import Income

class PersonTable(tables.Table):
    delete = tables.LinkColumn('main:delete_item', args=[A('delete-id')], attrs={'a': {'class': 'btn'}})
    class Meta:
        model = Income
        template_name = "django_tables2/bootstrap.html"

观看次数

from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import ListView
from .models import Income
from .tables import PersonTable
from .forms import IncomeModelForm

def homepage(request):
    table = PersonTable(Income.objects.all())

    if request.method == 'POST':
         form = IncomeModelForm(request.POST)
         if form.is_valid():
             print("Il form è valido")
             new_input = form.save()

    else :
        form = IncomeModelForm()

    context= {"form": form,
            "table":table }
    return render(request, "app/base.html", context)


def delete_item(request, pk):

    Income.objects.filter(id=pk).delete()
    items = Income.objects.all()
    context = {
    'items': items
    }
    return render(request, 'app/base.html', context)

html

{% load static %}
{% load render_table from django_tables2 %}
<!doctype html>
<html lang="it">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Hello, world!</title>
  </head>
  <div class="container">
        <form class="" action="" method="post">
            {% csrf_token %}
            {{form|crispy}}
            <input type="submit" class="btn btn-danger" value="INVIA">
        </form>
</div>

<br>
<br>

<div class="container">
    {% render_table table %}
</form>
</div>

  </body>
</html>

我的 table 显示列 "Delete" 但没有按钮,只有一个“-”。为什么?我的错误在哪里?

请添加这个

text='static text', 参数如 delete = tables.LinkColumn('main:delete_item',text='Delete', args=[A('delete-id')], attrs={'a': {'class': 'btn'}}) .

希望有用