0

I need your help please I'm trying to delete an specific record in my table that I already created, I have trying some codes but not working in my django version 1.10.4, Can someone help me?

Here's what I have:

Views.py

from django.shortcuts import render
from django.conf import settings
from .forms import regForm
from .models import registro
from registro.forms import regForm
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse



def test_reg(request):
    form = regForm(request.POST or None)
    queryset = registro.objects.all()
    # query_delete = queryset.delete()
    context = {
    "form": form,
    "queryset": queryset,
    }
    if form.is_valid():
        instance = form.save()
    return render(request, "registro.html", context)

def delete(request, id):
    note = get_object_or_404(registro, pk=id).delete()
    return HttpResponseRedirect(reverse('/'))

Template

<form method="POST" action="">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Registrame" />
</form>

<style>
table, th, td {
    border: 1px solid black;
}
</style>
<table>
    <tr>
        <th>Name</th>
        ...
        <th>Age</th>
        ...
        <th>Delete</th>
    </tr>
    {% for item in queryset %}
    <tr> 
        ...
        <td>{{ item.name }}</td> 
        ...
        <td>{{ item.age }}</td>
        ...
        <td> <a href="{% url 'delete' pk=registro.id %}">Delete</a> </td>
    </tr>
    {% endfor %}
</table>

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from myselect import views
from registro import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^select/', include('myselect.urls')),
    url(r'^delete/(?P<id>\d+)/$',views.delete, name='delete'),
    url(r'^$', views.test_reg, name='test_reg')
]

Model.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.
from django import forms

# Create your models here.

class registro(models.Model):
    name = models.CharField(max_length=100)
    age =  models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

Basically I have some users in my Database where I'm showing this through a table in my template, I want to delete the first one for example by clicking in my delete option where is in my last cell of my table

I tried this code and I got:

 Reverse for 'delete' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['delete/(?P<id>\\d+)/$']

How can I solve this and make it work good? Help please... thanks !

User100696
  • 687
  • 4
  • 12
  • 26
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – e4c5 Dec 14 '16 at 00:07
  • I'm just starting, I'm trying to delete any cell of my table and I don't figure out how to do that, I tried some codes I found but not working for me. I came here for help so I paste my entire code for solution. I'm sorry if this question was answered before. @e4c5 – User100696 Dec 14 '16 at 00:30

1 Answers1

1

The problem is in your template change:

<a href="{% url 'delete' pk=registro.id %}">Delete</a>

To:

<a href="{% url 'delete' item.id %}">Delete</a>
Messaoud Zahi
  • 1,214
  • 8
  • 15