简体   繁体   中英

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?

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

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

Here is my code.

byauthor.html --table is rendered in this 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'

And here is the main problem.

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

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.

Functionality is good, it works. Deletes the object with that 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'}
    })

I know i'm answering this very late (after 2 years), but it may help someone, like it helped me. The issue here was with the forloop and the wrong argument fetched in the template.

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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