1

How is going? I'm learning to programming in django. For the moment I'm building a simple app that utilizing a form update the referenced table.

Now I'm try to add a delete button in each row of my table but, beside I have tried a lot of solutions, I didn't find one that works correctly.

Below my code:

urls


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"),
]

forms

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"

views

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>

My table disply the column "Delete" but no buttoms, only a "-". Why? Where is my error?

1 Answer 1

2

When you display the record in a table you need to add an extra tabledata with delete form button. Eg:

<table>

{% for item in items%}

<tr>
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>
    <form method="delete">
        <input type="hidden" value="{{item.id}}" name="delete-id">
        <input type="Submit">Delete</button>
    </form>
</td>
</tr>

{% endfor}
</table>

Now, in your views.py add DELETE section and delete the record:

if request.method == 'DELETE':
        Person.objects.get(pk=request.DELETE['delete-id']).delete()

You are using django_tables2, you can simply add this code in your tables.py

from django_tables2.utils import A  # alias for Accessor
class PersonTable(tables.Table):
    delete = tables.LinkColumn('main:delete_item', args=[A('delete-id')], attrs={
    'a': {'class': 'btn'}
    })
Sign up to request clarification or add additional context in comments.

6 Comments

check if request.method is DELETE like you are checking for POST
I have edited the answer, I have changed method to DELETE instead of POST to make it more clear.
I have adjusted the code following your tips but dosn't work yet :(, I have modified the code of the post above.
I have followed the link and adjust the code (and I have updated it in this post) but the table disply the column "Delete" but no buttoms, only a "-". Why? Where is my error?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.