Repository: wagtail/wagtail

Issue:
Delete page in admin and get 404
### Issue Summary
When I delete the root page (a new project) I get a 404 in admin as after a delete Wagtail tries to go back to that page.



### Steps to Reproduce

1. start wagtail project
2. delete homepage that is made
3. get 404 page

I would expect to back to the three or see a screen that the page is deleted.

Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?

* I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes 


### Technical details

* Wagtail version: 2.11.1



Discussion:
Thanks for the report @onno-timmerman! I can confirm that I'm seeing the same when I initiate the deletion from http://localhost:8000/admin/pages/3/ (but not http://localhost:8000/admin/pages/).

Resolving patch:
diff --git a/wagtail/admin/wagtail_hooks.py b/wagtail/admin/wagtail_hooks.py
--- a/wagtail/admin/wagtail_hooks.py
+++ b/wagtail/admin/wagtail_hooks.py
@@ -210,6 +210,11 @@ def page_listing_more_buttons(page, page_perms, is_parent=False, next_url=None):
         )
     if page_perms.can_delete():
         url = reverse('wagtailadmin_pages:delete', args=[page.id])
+
+        # After deleting the page, it is impossible to redirect to it.
+        if next_url == reverse('wagtailadmin_explore', args=[page.id]):
+            next_url = None
+
         if next_url:
             url += '?' + urlencode({'next': next_url})
 
@@ -230,7 +235,6 @@ def page_listing_more_buttons(page, page_perms, is_parent=False, next_url=None):
             attrs={'title': _("Unpublish page '%(title)s'") % {'title': page.get_admin_display_title()}},
             priority=40
         )
-
     if page_perms.can_view_revisions():
         yield Button(
             _('History'),


Test patch:
diff --git a/wagtail/admin/tests/test_buttons_hooks.py b/wagtail/admin/tests/test_buttons_hooks.py
--- a/wagtail/admin/tests/test_buttons_hooks.py
+++ b/wagtail/admin/tests/test_buttons_hooks.py
@@ -1,12 +1,31 @@
 from django.test import TestCase
 from django.urls import reverse
+from django.utils.http import urlencode
 
 from wagtail.admin import widgets as wagtailadmin_widgets
+from wagtail.admin.wagtail_hooks import page_listing_more_buttons
 from wagtail.core import hooks
 from wagtail.core.models import Page
 from wagtail.tests.utils import WagtailTestUtils
 
 
+class PagePerms:
+    def can_move(self):
+        return False
+
+    def can_copy(self):
+        return False
+
+    def can_delete(self):
+        return True
+
+    def can_unpublish(self):
+        return False
+
+    def can_view_revisions(self):
+        return False
+
+
 class TestButtonsHooks(TestCase, WagtailTestUtils):
     def setUp(self):
         self.root_page = Page.objects.get(id=2)
@@ -81,3 +100,31 @@ def page_custom_listing_more_buttons(page, page_perms, is_parent=False, next_url
 
         self.assertContains(response, 'One more more button')
         self.assertContains(response, 'Another useless dropdown button in &quot;One more more button&quot; dropdown')
+
+    def test_delete_button_next_url(self):
+        page_perms = PagePerms()
+        page = self.root_page
+        base_url = reverse('wagtailadmin_pages:delete', args=[page.id])
+
+        next_url = "a/random/url/"
+        full_url = base_url + '?' + urlencode({'next': next_url})
+
+        # page_listing_more_button generator yields only `Delete button`
+        delete_button = next(page_listing_more_buttons(
+            page,
+            page_perms,
+            is_parent=False,
+            next_url=next_url
+        ))
+
+        self.assertEqual(delete_button.url, full_url)
+
+        next_url = reverse('wagtailadmin_explore', args=[page.id])
+        delete_button = next(page_listing_more_buttons(
+            page,
+            page_perms,
+            is_parent=False,
+            next_url=next_url
+        ))
+
+        self.assertEqual(delete_button.url, base_url)