7👍
How could that work for a delete view? How can Django redirect to the detail page of an object that no longer exists? That makes no sense at all; that page should return a 404.
I also don’t understand why you think you can’t reference the parent PK in the view. You certainly can:
def get_success_url(self):
return reverse ('parent-detail', kwargs={'pk': self.object.parent.pk})
4👍
I think you’re mixing up a few things here. get_absolute_url()
is expected to point to a view to the object it is called on (a Child instance in your case). From the docs:
Define a get_absolute_url() method to tell Django how to calculate the
canonical URL for an object.
The FormView’s behavior is to point to this URL after you’ve successfully edited an object. This makes sense, as you usually want to show the result of a user’s editing action to him.
The DeleteView however cannot do this, as, after successfully deleting the (child) object, it of course cannot be displayed anymore.
So, in short, don’t use get_absolute_url to point anywhere else as the object’s canonical view (child-detail
in your case I guess).
If you really want to re-use code in your case, you best write a small mixin class, e.g.:
class OnSuccessShowParent(object):
def get_success_url(self):
child = self.get_object()
return reverse ('parent-detail', kwargs = {'pk': str (child.parent.pk)})
and then mix it into your generic views:
class EditChild(FormView, OnSuccessShowParent):
...
class DeleteChild(DeleteView, OnSuccessShowParent):
...
NB: as the object is deleted after get_success_url() is called, this is perfectly fine.
- [Django]-Django – Exception Value: Unexpected end of expression in if tag
- [Django]-Django: related_name attribute (DatabaseError)
- [Django]-Show children nodes depending on selected parent
- [Django]-Django: How to keep track of a linear (yet flexible) project management workflow?