我需要你的帮助,请我尝试删除我已经创建的表格中的特定记录,我尝试了一些代码但不能使用我的django版本1.10.4
,有人可以帮助我吗?
以下是我所拥有的:
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('/'))
模板
<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
基本上我的Database
中有一些用户在我的模板中通过table
显示此内容,我想删除第一个用户,例如点击我的删除选项是我table
我尝试了这段代码,然后得到了:
Reverse for 'delete' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['delete/(?P<id>\\d+)/$']
如何解决这个问题并让它运作良好? 请帮助...谢谢!
答案 0 :(得分:1)
问题在于您的模板更改:
<a href="{% url 'delete' pk=registro.id %}">Delete</a>
要:
<a href="{% url 'delete' item.id %}">Delete</a>