8

我有类人

class People (models.Model):
    first_name = models.CharField(max_length = 50)
    last_name = models.CharField(max_length = 50)
    email = models.EmailField(blank = True)
    grade = models.CharField(max_length = 2)

    def __unicode__(self):
        return '%s %s' % (self.first_name, self.last_name)

我有删除视图

def delete(request):
    query = People.objects.get(pk=id)
    query.delete()
    return HttpResponse("Deleted!")

我有 html 模板代码

{% for person in people_list %}
<TR ALIGN="CENTER">
        <td>{{ person.first_name }}</td>
    <td>{{ person.last_name }}</td>
    <td>{{ person.email }}</td>
    <td>{{ person.grade }}</td>
    <td><form action="/modify.html">
        <input type="submit" value="Modify">
        </form></td>
    <td><form action="/delete.html">
        <input type="submit" value="Delete">
        </form></td>
</TR>
      {% endfor %}

如何从模板中获取 person.id 并将其放入删除视图中并删除与我想要的 person.id 对应的对象。

4

5 回答 5

8

您需要让您的delete函数(顺便说一句,将其命名为其他名称,例如delete_person)接受一个参数 pk。

def delete_person(request, pk):
    ...
    ...

然后在你的urlconf中,做这样的事情

url(r'^delete_person/(?P<pk>\d+)/$', 'delete_person', name='delete_person')

然后像这样在模板中生成url

{% url 'delete-person' person.id %}

PS - 无需让您的网址以.html

PPS - 在视图中进行一些验证以确保用户有权删除此人是个好主意。

于 2013-11-03T14:47:52.843 回答
4

我认为您可以使用链接而不是表单:

替换此行

<td>
  <form action="/delete.html">
    <input type="submit" value="Delete">
  </form>
</td>

有了这个

<td><a href="/delete/{{ person.id }}">Delete</a></td>

在 urls.py 中,您应该添加以下行以将您的视图与 url 关联:

url(r'^delete/(?P<person_pk>.*)$', 'person.views.delete' name='delete-person'),

然后改变你的看法:

def delete(request, person_pk):
    query = People.objects.get(pk=person_pk)
    query.delete()
    return HttpResponse("Deleted!")
于 2013-11-03T14:51:31.890 回答
2

为了扩展@scriptmonster,我尝试将查询包装起来,但要正确返回 404

# urls.py
path("foo/<int:pk>/", views.ModelView.as_view(), name="foo-detail"),$                                     

# views.py
from django.http import HttpResponse, HttpResponseNotFound                                               

class ModelView(View):                                                                                             
    model = Foo                                                                                                                                                                                                                                                     

    def delete(self, request, *args, **kwargs):                                                                       
        foo_id = kwargs["pk"]                                                                                      
        try:                                                                                                          
            query = Foo.objects.get(pk=foo_id)                                                                  
            query.delete()                                                                                            
            return HttpResponse("Deleted!")                                                                           
        except:                                                                                                       
            return HttpResponseNotFound()   
于 2018-07-08T01:57:52.873 回答
0
from test_app.(your Application Name) import views

path('delete/(?p<id>\d+)/',views.delete_View(),
于 2019-09-25T21:35:52.277 回答
0

通过基于类的视图删除对象:

视图.py

from django.views import generic
from django.urls import reverse_lazy
class PeopleDeleteView(generic.edit.DeleteView):
    model = People
    success_url = reverse_lazy('people_list')

网址.py

...
path('delete/<int:pk>/', views.PeopleDeleteView.as_view(), name='delete'),

您需要一个删除确认模板:

myapp/people_confirmation_delete.html

<form method="post">{% csrf_token %}
    <p>Are you sure you want to delete "{{ object }}"?</p>
    <input type="submit" value="Confirm">
</form>
于 2021-06-16T19:46:35.820 回答