简体   繁体   English

带有编辑和删除按钮的 Django_tables2。 如何正确操作?

[英]Django_tables2 with Edit and Delete buttons. How to do it properly?

I'm building an app that lists, sorts, updates and deletes objects.我正在构建一个列出、排序、更新和删除对象的应用程序。 How can I properly add edit and/or delete buttons to django-tables2 table render?如何将编辑和/或删除按钮正确添加到 django-tables2 表渲染?

Python version: 3.7 and Django version: 2.1.7 are used.使用 Python 版本:3.7 和 Django 版本:2.1.7。

I have tried multiple ways and searched on internet but it seems a little complicated to implement it with django-tables2 table rendering.我尝试了多种方法并在互联网上搜索,但使用 django-tables2 表渲染来实现它似乎有点复杂。

Here is my code.这是我的代码。

byauthor.html --table is rendered in this html byauthor.html --table 在此 html 中呈现

{% extends "main/base.html" %}

{% block content %}

{% load render_table from django_tables2 %}

            <h3>Logged in: {{user.first_name}} {{user.last_name}} </h3>
            <p>{{ time|date:"d.m.Y." }}</p>

            {% render_table table %}

{% endblock %}

views.py视图.py

def byauthor(request):
    current_account = request.user

    items = Cashier.objects.filter(user__exact=current_account).filter(cashier_published__date=datetime.today())
    table = CashierTable(Cashier.objects.filter(user__exact=current_account).filter(cashier_published__date=datetime.today()))

    RequestConfig(request).configure(table)

    return render(request, 'main/byauthor.html', {'table': table, 'time': datetime.now(), 'items': items})

def delete_item(request, pk):

    Cashier.objects.filter(id=pk).delete()

    items = Cashier.objects.all()

    context = {
    'items': items
    }

    return render(request, 'main/delete_confirmation.html', context)

urls.py网址.py

from django.urls import path
from . import views


app_name = 'main'  # here for namespacing of urls.

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

Here I have added a column to a table model.在这里,我向表模型添加了一列。

tables.py表.py

class CashierTable(tables.Table):
    delete = tables.TemplateColumn(template_name='main/delete_template.html', orderable=False)

    class Meta:
        model = Cashier
        order_by = '-id'

And here is the main problem.这是主要问题。

delete_template.html delete_template.html

{% for item in items %}
    <a href="{% url 'main:delete_item' item.pk %}" type="submit" class="btn"><button>{{ item.id }}</button></a>
{% endfor %}

When my table gets rendered out it obviously iterates through objects for which it generates a new row, and that is fine.当我的表被渲染出来时,它显然会遍历它生成新行的对象,这很好。 But when I render it with this delete_template.html which represents the button to delete specific object, it iterates again through objects and generates buttons for all objects in each row.但是当我用这个 delete_template.html 呈现它时,它代表删除特定对象的按钮,它再次遍历对象并为每一行中的所有对象生成按钮。 So if I have 10 objects it generates 10 delete buttons for each row.因此,如果我有 10 个对象,它会为每一行生成 10 个删除按钮。

But if I delete this {% for %} loop in delete_template.html it produces this error:但是如果我在delete_template.html 中删除这个 {% for %} 循环,它会产生这个错误:

NoReverseMatch at /byauthor
Reverse for 'delete_item' with arguments '('',)' not found. 1 pattern(s) tried: ['byauthor/delete_item/(?P<pk>[0-9]+)$']

Any help or tips would be appreciated.任何帮助或提示将不胜感激。

Functionality is good, it works.功能很好,很管用。 Deletes the object with that ID.删除具有该 ID 的对象。

My goal is to generate one button for each object(row) which has object's ID in it so I can forward it to deletion by clicking it.我的目标是为每个包含对象 ID 的对象(行)生成一个按钮,以便我可以通过单击将其转发到删除。

I think you can use LinkColumn to add a delete button.我认为您可以使用LinkColumn添加删除按钮。 You can do it like this:你可以这样做:

from django_tables2.utils import A  # alias for Accessor


class CashierTable(tables.Table):
    delete = = tables.LinkColumn('main:delete_item', args=[A('pk')], attrs={
    'a': {'class': 'btn'}
    })

I know i'm answering this very late (after 2 years), but it may help someone, like it helped me.我知道我很晚才回答这个问题(2 年后),但它可能会帮助某人,就像它帮助了我一样。 The issue here was with the forloop and the wrong argument fetched in the template.这里的问题是 forloop 和模板中获取的错误参数。

<a href="{% url 'main:delete_item' record.pk %}" type="submit" class="btn"><button>{{ item.id }}</button></a>

the argument that should be fetched is "record" to access the actual object being rendered in that row.应该获取的参数是“记录”以访问在该行中呈现的实际对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM