Redirect to delete view in Django admin

老子叫甜甜 提交于 2019-12-11 11:55:36

问题


I'm working on a Django/Wagtail project. I have a very customized feature in the admin view what will delete an object when hitting Save if certain conditions are met.

I have this in my class (that is a snippet):

def save(self, *args, **kwargs):

    if condition:     
        self.delete()

    else:
        return super(MyModel, self).save(*args, **kwargs)

This works as expected. The object gets deleted, but after this I get an error page because the browser remains in the same URL /snippets/myapp/mymodel/1234/ but the object doesn't exist anymore.

I thought of two possible solutions for this.

Possible solution 1:

def save(self, *args, **kwargs):

if condition:     
    self.delete()

    #### ===== > Redirect to objects list view

else:
    return super(MyModel, self).save(*args, **kwargs)

Possible solution 2:

def save(self, *args, **kwargs):

if condition:     

    #### ===== > Don't delete and redirect to delete view (.../mymodel/1234/delete/)

else:
    return super(MyModel, self).save(*args, **kwargs)

These are the two possible solutions I thought, but I don't know how to do any of them, even after reading docs here and there.

How can I do what I'm trying to achieve?

来源:https://stackoverflow.com/questions/49824630/redirect-to-delete-view-in-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!