带有“编辑"和“删除"按钮的Django_tables2.如何正确地做? [英] Django_tables2 with Edit and Delete buttons. How to do it properly?

查看:25
本文介绍了带有“编辑"和“删除"按钮的Django_tables2.如何正确地做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个列出,排序,更新和删除对象的应用程序.如何正确地向django-tables2表渲染添加编辑和/或删除按钮?

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?

使用Python版本:3.7和Django版本:2.1.7.

Python version: 3.7 and Django version: 2.1.7 are used.

我尝试了多种方法并在Internet上进行了搜索,但是使用django-tables2表呈现来实现它似乎有些复杂.

I have tried multiple ways and searched on internet but it seems a little complicated to implement it with django-tables2 table rendering.

这是我的代码.

byauthor.html -表在此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

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

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

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

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

这是主要问题.

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

当我的表被渲染时,它显然会遍历对象,并为其生成新行,这很好.但是,当我使用代表代表删除特定对象的按钮的delete_template.html进行渲染时,它将再次遍历对象并为每一行中的所有对象生成按钮.因此,如果我有10个对象,它将为每行生成10个删除按钮.

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. So if I have 10 objects it generates 10 delete buttons for each row.

但是,如果我在 delete_template.html 中删除此{%for%}循环,则会产生此错误:

But if I delete this {% for %} loop in delete_template.html it produces this error:

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.

功能不错,可以正常工作.删除具有该ID的对象.

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.

推荐答案

我认为您可以使用

I think you can use LinkColumn to add a delete button. 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'}
    })

这篇关于带有“编辑"和“删除"按钮的Django_tables2.如何正确地做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆