Repository: wagtail/wagtail

Issue:
Renaming homepage breaks 'View Live' link
We've got a structure similar to the following:
- Homepage
  - Page 1
  - Page 2
  - Page 3

Whenever I change the title (or slug for that matter) of the homepage the _View Live_ link will break to something along the lines of http://example.com/admin/pages/None instead of http://example.com. Also, the _View Live_ links of the child pages break too, e.g. http://example.com/admin/pages/3/None instead of http://example.com/page-1.

Only once I edit and save (no changes) the **localhost [default]** entry in Django admin within **Sites** the link will start working again.



Discussion:
Hi @christianklotz, Thanks for reporting.

This may be related to: https://github.com/torchbox/wagtail/issues/7


Resolving patch:
diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py
--- a/wagtail/wagtailcore/models.py
+++ b/wagtail/wagtailcore/models.py
@@ -302,6 +302,11 @@ def save(self, *args, **kwargs):
 
         if update_descendant_url_paths:
             self._update_descendant_url_paths(old_url_path, new_url_path)
+
+        # Check if this is a root page of any sites and clear the 'wagtail_site_root_paths' key if so
+        if Site.objects.filter(root_page=self).exists():
+            cache.delete('wagtail_site_root_paths')
+
         return result
 
     def _update_descendant_url_paths(self, old_url_path, new_url_path):


Test patch:
diff --git a/wagtail/wagtailcore/tests.py b/wagtail/wagtailcore/tests.py
--- a/wagtail/wagtailcore/tests.py
+++ b/wagtail/wagtailcore/tests.py
@@ -603,3 +603,79 @@ def test_move_page(self):
         christmas = events_index.get_children().get(slug='christmas')
         self.assertEqual(christmas.depth, 5)
         self.assertEqual(christmas.url_path, '/home/about-us/events/christmas/')
+
+
+class TestIssue7(TestCase):
+    """
+    This tests for an issue where if a site root page was moved, all the page 
+    urls in that site would change to None.
+
+    The issue was caused by the 'wagtail_site_root_paths' cache variable not being
+    cleared when a site root page was moved. Which left all the child pages
+    thinking that they are no longer in the site and return None as their url.
+
+    Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682
+    Discussion: https://github.com/torchbox/wagtail/issues/7
+    """
+
+    fixtures = ['test.json']
+
+    def test_issue7(self):
+        # Get homepage, root page and site
+        root_page = Page.objects.get(id=1)
+        homepage = Page.objects.get(url_path='/home/')
+        default_site = Site.objects.get(is_default_site=True)
+
+        # Create a new homepage under current homepage
+        new_homepage = SimplePage(title="New Homepage", slug="new-homepage")
+        homepage.add_child(instance=new_homepage)
+
+        # Set new homepage as the site root page
+        default_site.root_page = new_homepage
+        default_site.save()
+
+        # Warm up the cache by getting the url
+        _ = homepage.url
+
+        # Move new homepage to root
+        new_homepage.move(root_page, pos='last-child')
+
+        # Get fresh instance of new_homepage
+        new_homepage = Page.objects.get(id=new_homepage.id)
+
+        # Check url
+        self.assertEqual(new_homepage.url, '/')
+
+
+class TestIssue157(TestCase):
+    """
+    This tests for an issue where if a site root pages slug was changed, all the page 
+    urls in that site would change to None.
+
+    The issue was caused by the 'wagtail_site_root_paths' cache variable not being
+    cleared when a site root page was changed. Which left all the child pages
+    thinking that they are no longer in the site and return None as their url.
+
+    Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682
+    Discussion: https://github.com/torchbox/wagtail/issues/157
+    """
+
+    fixtures = ['test.json']
+
+    def test_issue157(self):
+        # Get homepage
+        homepage = Page.objects.get(url_path='/home/')
+
+        # Warm up the cache by getting the url
+        _ = homepage.url
+
+        # Change homepage title and slug
+        homepage.title = "New home"
+        homepage.slug = "new-home"
+        homepage.save()
+
+        # Get fresh instance of homepage
+        homepage = Page.objects.get(id=homepage.id)
+
+        # Check url
+        self.assertEqual(homepage.url, '/')