简体   繁体   English

如何将其他URL值传递给Django通用DeleteView?

[英]How do you pass additional URL values to Django generic DeleteView?

Suppose I have models.py: 假设我有models.py:

class Parent(models.Model):
    name = models.CharField(max_length=20)

class child(models.Model):
    name = models.CharField(max_length=20)
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE)

Now in the Detail View of a Parent I list out the children that belong to the parent. 现在,在“父级”的“详细信息视图”中,我列出了属于父级的子级。

The urls.py for this would look like 的urls.py看起来像

path('parents/<int:pk>/, views.ParentDetailView.as_view(), name='parent-detail')

And I'd use a django DetailView in my views.py like this 我会像这样在我的views.py中使用Django DetailView

class ParentDetailView(DetailView):
    model = Parent

Now in this detail view I list out the children of the parent with something like parent_detail.html in my templates 现在,在此详细信息视图中,我在模板中列出了带有parent_detail.html之类的父项的子项

{{parent}}
{% for child in parent.child_set.all %}
    {{ child.name }}
{% endfor %}

But I'd like to be able to delete the child from the database from here on this page, so I'd add something like 但我希望能够从此页面上的此处从数据库中删除该子项,因此我需要添加类似

{{parent}}
{% for child in parent.child_set.all %}
    {{ child.name }}
    <a href="{% url 'myapp:parent-delete-child" parent.pk child.pk %}">Delete child</a>
{% endfor %}

Here's where I'm stuck! 这就是我卡住的地方! I would love to have something like this in my urls.py 我希望在urls.py中有这样的内容

path('parents/<int:pk>/', views.ParentDetailView.as_view(), name='parent-detail'),
path('parents/<int:pk>/delete_child/<int:child_pk>/', views.ParentDeleteChildView.as_view(), name='parent-delete-child')

But I have no idea how to send the pk and the child_pk to the generic django DeleteView?!?!?! 但是我不知道如何将pk和child_pk发送到通用django DeleteView吗?!?!?!

class ParentDeleteChildView(DeleteView):
    model = Child
    success_url = reverse_lazy('myapp:parent-detail' kwargs={'pk':pk})

After deleting I want to go back to the parent detail page. 删除后,我想返回到父详细信息页面。 But the success url needs to know the pk of the parent. 但是成功网址需要知道父级的pk。 How do I tell the generic view to delete the child that matches the child_pk and then go to the parent detail page that matches the pk? 如何告诉通用视图删除与child_pk匹配的子项,然后转到与pk匹配的父级详细信息页面? Am I better off not using the generic DeleteView? 我最好不要使用通用DeleteView吗?

Thanks in advance! 提前致谢!

We can achieve it using get_success_url in django . 我们可以使用django get_success_url来实现。 By default pk_url_kwarg is set to kwarg pk . 默认情况下, pk_url_kwarg设置为kwarg pk But in this case we have to delete child object ie child_pk . 但是在这种情况下,我们必须删除子对象,即child_pk so, we have to mention it by overriding pk_url_kwarg to child_pk . 所以,我们必须通过重写提到它pk_url_kwargchild_pk

class ParentDeleteChildView(DeleteView):
    model = Child
    pk_url_kwarg = 'child_pk'

    def get_success_url(self):
        success_url = reverse_lazy('myapp:parent-detail' kwargs={'pk':self.kwargs['pk']})
        return success_url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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