We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
PathOverflow: Path Overflow from: '**********ZZZZ'
### Issue Summary

Creating a new page from the administration area raises a PathOverflow error.

```
PathOverflow: Path Overflow from: '000100020003ZZZZ'
  File "django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "wagtail/admin/urls/__init__.py", line 102, in wrapper
    return view_func(request, *args, **kwargs)
  File "wagtail/admin/decorators.py", line 34, in decorated_view
    return view_func(request, *args, **kwargs)
  File "wagtail/admin/views/pages.py", line 224, in create
    parent_page.add_child(instance=page)
  File "treebeard/mp_tree.py", line 1013, in add_child
    return MP_AddChildHandler(self, **kwargs).process()
  File "treebeard/mp_tree.py", line 387, in process
    newobj.path = self.node.get_last_child()._inc_path()
  File "treebeard/mp_tree.py", line 1114, in _inc_path
    raise PathOverflow(_("Path Overflow from: '%s'" % (self.path, )))
```

The only information I could find potentially regarding the issue was on SO at the following link: https://stackoverflow.com/questions/54166821/wagtail-pathoverflow-on-adding-new-child-page

There are only a handful of pages, so I'm not sure that particular issue applies though.

### Steps to Reproduce

I haven't been able to reproduce this myself, however a client has been seeing this behavior, which at the time I've just resolved by modifying the ZZZZ at the end to be numeric. I'm in the process of gathering some steps they may have taken to replicate it from this end (where they tried to add the page from)

* I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: no


### Technical details

* Python version: 3.6.8
* Django version: 2.1.4
* Wagtail version: 2.4
* Browser version: N/A


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `wagtail/admin/views/pages/preview.py`
Content:
```
1 from time import time
2 
3 from django.contrib.contenttypes.models import ContentType
4 from django.core.exceptions import PermissionDenied
5 from django.http import Http404, JsonResponse
6 from django.http.request import QueryDict
7 from django.shortcuts import get_object_or_404
8 from django.template.response import TemplateResponse
9 from django.views.generic import View
10 
11 from wagtail.core.models import Page
12 
13 
14 def view_draft(request, page_id):
15     page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page()
16     perms = page.permissions_for_user(request.user)
17     if not (perms.can_publish() or perms.can_edit()):
18         raise PermissionDenied
19 
20     try:
21         preview_mode = page.default_preview_mode
22     except IndexError:
23         raise PermissionDenied
24 
25     return page.make_preview_request(request, preview_mode)
26 
27 
28 class PreviewOnEdit(View):
29     http_method_names = ('post', 'get')
30     preview_expiration_timeout = 60 * 60 * 24  # seconds
31     session_key_prefix = 'wagtail-preview-'
32 
33     def remove_old_preview_data(self):
34         expiration = time() - self.preview_expiration_timeout
35         expired_keys = [
36             k for k, v in self.request.session.items()
37             if k.startswith(self.session_key_prefix) and v[1] < expiration]
38         # Removes the session key gracefully
39         for k in expired_keys:
40             self.request.session.pop(k)
41 
42     @property
43     def session_key(self):
44         return self.session_key_prefix + ','.join(self.args)
45 
46     def get_page(self):
47         return get_object_or_404(Page,
48                                  id=self.kwargs["page_id"]).get_latest_revision_as_page()
49 
50     def get_form(self, page, query_dict):
51         form_class = page.get_edit_handler().get_form_class()
52         parent_page = page.get_parent().specific
53 
54         if self.session_key not in self.request.session:
55             # Session key not in session, returning null form
56             return form_class(instance=page, parent_page=parent_page)
57 
58         return form_class(query_dict, instance=page, parent_page=parent_page)
59 
60     def post(self, request, *args, **kwargs):
61         # TODO: Handle request.FILES.
62         request.session[self.session_key] = request.POST.urlencode(), time()
63         self.remove_old_preview_data()
64         form = self.get_form(self.get_page(), request.POST)
65         return JsonResponse({'is_valid': form.is_valid()})
66 
67     def error_response(self, page):
68         return TemplateResponse(
69             self.request, 'wagtailadmin/pages/preview_error.html',
70             {'page': page}
71         )
72 
73     def get(self, request, *args, **kwargs):
74         page = self.get_page()
75 
76         post_data, timestamp = self.request.session.get(self.session_key,
77                                                         (None, None))
78         if not isinstance(post_data, str):
79             post_data = ''
80         form = self.get_form(page, QueryDict(post_data))
81 
82         if not form.is_valid():
83             return self.error_response(page)
84 
85         form.save(commit=False)
86 
87         try:
88             preview_mode = request.GET.get('mode', page.default_preview_mode)
89         except IndexError:
90             raise PermissionDenied
91 
92         return page.make_preview_request(request, preview_mode)
93 
94 
95 class PreviewOnCreate(PreviewOnEdit):
96     def get_page(self):
97         content_type_app_name = self.kwargs["content_type_app_name"]
98         content_type_model_name = self.kwargs["content_type_model_name"]
99         parent_page_id = self.kwargs["parent_page_id"]
100         try:
101             content_type = ContentType.objects.get_by_natural_key(
102                 content_type_app_name, content_type_model_name)
103         except ContentType.DoesNotExist:
104             raise Http404
105 
106         page = content_type.model_class()()
107         parent_page = get_object_or_404(Page, id=parent_page_id).specific
108         # We need to populate treebeard's path / depth fields in order to
109         # pass validation. We can't make these 100% consistent with the rest
110         # of the tree without making actual database changes (such as
111         # incrementing the parent's numchild field), but by calling treebeard's
112         # internal _get_path method, we can set a 'realistic' value that will
113         # hopefully enable tree traversal operations
114         # to at least partially work.
115         page.depth = parent_page.depth + 1
116         # Puts the page at the maximum possible path
117         # for a child of `parent_page`.
118         page.path = Page._get_children_path_interval(parent_page.path)[1]
119         return page
120 
121     def get_form(self, page, query_dict):
122         form = super().get_form(page, query_dict)
123         if form.is_valid():
124             # Ensures our unsaved page has a suitable url.
125             form.instance.set_url_path(form.parent_page)
126 
127             form.instance.full_clean()
128         return form
129
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/wagtail/admin/views/pages/preview.py b/wagtail/admin/views/pages/preview.py
--- a/wagtail/admin/views/pages/preview.py
+++ b/wagtail/admin/views/pages/preview.py
@@ -113,9 +113,15 @@
         # hopefully enable tree traversal operations
         # to at least partially work.
         page.depth = parent_page.depth + 1
-        # Puts the page at the maximum possible path
+        # Puts the page at the next available path
         # for a child of `parent_page`.
-        page.path = Page._get_children_path_interval(parent_page.path)[1]
+        if parent_page.is_leaf():
+            # set the path as the first child of parent_page
+            page.path = page._get_path(parent_page.path, page.depth, 1)
+        else:
+            # add the new page after the last child of parent_page
+            page.path = parent_page.get_last_child()._inc_path()
+
         return page
 
     def get_form(self, page, query_dict):

{"golden_diff": "diff --git a/wagtail/admin/views/pages/preview.py b/wagtail/admin/views/pages/preview.py\n--- a/wagtail/admin/views/pages/preview.py\n+++ b/wagtail/admin/views/pages/preview.py\n@@ -113,9 +113,15 @@\n         # hopefully enable tree traversal operations\n         # to at least partially work.\n         page.depth = parent_page.depth + 1\n-        # Puts the page at the maximum possible path\n+        # Puts the page at the next available path\n         # for a child of `parent_page`.\n-        page.path = Page._get_children_path_interval(parent_page.path)[1]\n+        if parent_page.is_leaf():\n+            # set the path as the first child of parent_page\n+            page.path = page._get_path(parent_page.path, page.depth, 1)\n+        else:\n+            # add the new page after the last child of parent_page\n+            page.path = parent_page.get_last_child()._inc_path()\n+\n         return page\n \n     def get_form(self, page, query_dict):\n", "issue": "PathOverflow: Path Overflow from: '**********ZZZZ'\n### Issue Summary\r\n\r\nCreating a new page from the administration area raises a PathOverflow error.\r\n\r\n```\r\nPathOverflow: Path Overflow from: '000100020003ZZZZ'\r\n  File \"django/core/handlers/exception.py\", line 34, in inner\r\n    response = get_response(request)\r\n  File \"django/core/handlers/base.py\", line 126, in _get_response\r\n    response = self.process_exception_by_middleware(e, request)\r\n  File \"django/core/handlers/base.py\", line 124, in _get_response\r\n    response = wrapped_callback(request, *callback_args, **callback_kwargs)\r\n  File \"django/views/decorators/cache.py\", line 44, in _wrapped_view_func\r\n    response = view_func(request, *args, **kwargs)\r\n  File \"wagtail/admin/urls/__init__.py\", line 102, in wrapper\r\n    return view_func(request, *args, **kwargs)\r\n  File \"wagtail/admin/decorators.py\", line 34, in decorated_view\r\n    return view_func(request, *args, **kwargs)\r\n  File \"wagtail/admin/views/pages.py\", line 224, in create\r\n    parent_page.add_child(instance=page)\r\n  File \"treebeard/mp_tree.py\", line 1013, in add_child\r\n    return MP_AddChildHandler(self, **kwargs).process()\r\n  File \"treebeard/mp_tree.py\", line 387, in process\r\n    newobj.path = self.node.get_last_child()._inc_path()\r\n  File \"treebeard/mp_tree.py\", line 1114, in _inc_path\r\n    raise PathOverflow(_(\"Path Overflow from: '%s'\" % (self.path, )))\r\n```\r\n\r\nThe only information I could find potentially regarding the issue was on SO at the following link: https://stackoverflow.com/questions/54166821/wagtail-pathoverflow-on-adding-new-child-page\r\n\r\nThere are only a handful of pages, so I'm not sure that particular issue applies though.\r\n\r\n### Steps to Reproduce\r\n\r\nI haven't been able to reproduce this myself, however a client has been seeing this behavior, which at the time I've just resolved by modifying the ZZZZ at the end to be numeric. I'm in the process of gathering some steps they may have taken to replicate it from this end (where they tried to add the page from)\r\n\r\n* I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: no\r\n\r\n\r\n### Technical details\r\n\r\n* Python version: 3.6.8\r\n* Django version: 2.1.4\r\n* Wagtail version: 2.4\r\n* Browser version: N/A\r\n\n", "before_files": [{"content": "from time import time\n\nfrom django.contrib.contenttypes.models import ContentType\nfrom django.core.exceptions import PermissionDenied\nfrom django.http import Http404, JsonResponse\nfrom django.http.request import QueryDict\nfrom django.shortcuts import get_object_or_404\nfrom django.template.response import TemplateResponse\nfrom django.views.generic import View\n\nfrom wagtail.core.models import Page\n\n\ndef view_draft(request, page_id):\n    page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page()\n    perms = page.permissions_for_user(request.user)\n    if not (perms.can_publish() or perms.can_edit()):\n        raise PermissionDenied\n\n    try:\n        preview_mode = page.default_preview_mode\n    except IndexError:\n        raise PermissionDenied\n\n    return page.make_preview_request(request, preview_mode)\n\n\nclass PreviewOnEdit(View):\n    http_method_names = ('post', 'get')\n    preview_expiration_timeout = 60 * 60 * 24  # seconds\n    session_key_prefix = 'wagtail-preview-'\n\n    def remove_old_preview_data(self):\n        expiration = time() - self.preview_expiration_timeout\n        expired_keys = [\n            k for k, v in self.request.session.items()\n            if k.startswith(self.session_key_prefix) and v[1] < expiration]\n        # Removes the session key gracefully\n        for k in expired_keys:\n            self.request.session.pop(k)\n\n    @property\n    def session_key(self):\n        return self.session_key_prefix + ','.join(self.args)\n\n    def get_page(self):\n        return get_object_or_404(Page,\n                                 id=self.kwargs[\"page_id\"]).get_latest_revision_as_page()\n\n    def get_form(self, page, query_dict):\n        form_class = page.get_edit_handler().get_form_class()\n        parent_page = page.get_parent().specific\n\n        if self.session_key not in self.request.session:\n            # Session key not in session, returning null form\n            return form_class(instance=page, parent_page=parent_page)\n\n        return form_class(query_dict, instance=page, parent_page=parent_page)\n\n    def post(self, request, *args, **kwargs):\n        # TODO: Handle request.FILES.\n        request.session[self.session_key] = request.POST.urlencode(), time()\n        self.remove_old_preview_data()\n        form = self.get_form(self.get_page(), request.POST)\n        return JsonResponse({'is_valid': form.is_valid()})\n\n    def error_response(self, page):\n        return TemplateResponse(\n            self.request, 'wagtailadmin/pages/preview_error.html',\n            {'page': page}\n        )\n\n    def get(self, request, *args, **kwargs):\n        page = self.get_page()\n\n        post_data, timestamp = self.request.session.get(self.session_key,\n                                                        (None, None))\n        if not isinstance(post_data, str):\n            post_data = ''\n        form = self.get_form(page, QueryDict(post_data))\n\n        if not form.is_valid():\n            return self.error_response(page)\n\n        form.save(commit=False)\n\n        try:\n            preview_mode = request.GET.get('mode', page.default_preview_mode)\n        except IndexError:\n            raise PermissionDenied\n\n        return page.make_preview_request(request, preview_mode)\n\n\nclass PreviewOnCreate(PreviewOnEdit):\n    def get_page(self):\n        content_type_app_name = self.kwargs[\"content_type_app_name\"]\n        content_type_model_name = self.kwargs[\"content_type_model_name\"]\n        parent_page_id = self.kwargs[\"parent_page_id\"]\n        try:\n            content_type = ContentType.objects.get_by_natural_key(\n                content_type_app_name, content_type_model_name)\n        except ContentType.DoesNotExist:\n            raise Http404\n\n        page = content_type.model_class()()\n        parent_page = get_object_or_404(Page, id=parent_page_id).specific\n        # We need to populate treebeard's path / depth fields in order to\n        # pass validation. We can't make these 100% consistent with the rest\n        # of the tree without making actual database changes (such as\n        # incrementing the parent's numchild field), but by calling treebeard's\n        # internal _get_path method, we can set a 'realistic' value that will\n        # hopefully enable tree traversal operations\n        # to at least partially work.\n        page.depth = parent_page.depth + 1\n        # Puts the page at the maximum possible path\n        # for a child of `parent_page`.\n        page.path = Page._get_children_path_interval(parent_page.path)[1]\n        return page\n\n    def get_form(self, page, query_dict):\n        form = super().get_form(page, query_dict)\n        if form.is_valid():\n            # Ensures our unsaved page has a suitable url.\n            form.instance.set_url_path(form.parent_page)\n\n            form.instance.full_clean()\n        return form\n", "path": "wagtail/admin/views/pages/preview.py"}], "after_files": [{"content": "from time import time\n\nfrom django.contrib.contenttypes.models import ContentType\nfrom django.core.exceptions import PermissionDenied\nfrom django.http import Http404, JsonResponse\nfrom django.http.request import QueryDict\nfrom django.shortcuts import get_object_or_404\nfrom django.template.response import TemplateResponse\nfrom django.views.generic import View\n\nfrom wagtail.core.models import Page\n\n\ndef view_draft(request, page_id):\n    page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page()\n    perms = page.permissions_for_user(request.user)\n    if not (perms.can_publish() or perms.can_edit()):\n        raise PermissionDenied\n\n    try:\n        preview_mode = page.default_preview_mode\n    except IndexError:\n        raise PermissionDenied\n\n    return page.make_preview_request(request, preview_mode)\n\n\nclass PreviewOnEdit(View):\n    http_method_names = ('post', 'get')\n    preview_expiration_timeout = 60 * 60 * 24  # seconds\n    session_key_prefix = 'wagtail-preview-'\n\n    def remove_old_preview_data(self):\n        expiration = time() - self.preview_expiration_timeout\n        expired_keys = [\n            k for k, v in self.request.session.items()\n            if k.startswith(self.session_key_prefix) and v[1] < expiration]\n        # Removes the session key gracefully\n        for k in expired_keys:\n            self.request.session.pop(k)\n\n    @property\n    def session_key(self):\n        return self.session_key_prefix + ','.join(self.args)\n\n    def get_page(self):\n        return get_object_or_404(Page,\n                                 id=self.kwargs[\"page_id\"]).get_latest_revision_as_page()\n\n    def get_form(self, page, query_dict):\n        form_class = page.get_edit_handler().get_form_class()\n        parent_page = page.get_parent().specific\n\n        if self.session_key not in self.request.session:\n            # Session key not in session, returning null form\n            return form_class(instance=page, parent_page=parent_page)\n\n        return form_class(query_dict, instance=page, parent_page=parent_page)\n\n    def post(self, request, *args, **kwargs):\n        # TODO: Handle request.FILES.\n        request.session[self.session_key] = request.POST.urlencode(), time()\n        self.remove_old_preview_data()\n        form = self.get_form(self.get_page(), request.POST)\n        return JsonResponse({'is_valid': form.is_valid()})\n\n    def error_response(self, page):\n        return TemplateResponse(\n            self.request, 'wagtailadmin/pages/preview_error.html',\n            {'page': page}\n        )\n\n    def get(self, request, *args, **kwargs):\n        page = self.get_page()\n\n        post_data, timestamp = self.request.session.get(self.session_key,\n                                                        (None, None))\n        if not isinstance(post_data, str):\n            post_data = ''\n        form = self.get_form(page, QueryDict(post_data))\n\n        if not form.is_valid():\n            return self.error_response(page)\n\n        form.save(commit=False)\n\n        try:\n            preview_mode = request.GET.get('mode', page.default_preview_mode)\n        except IndexError:\n            raise PermissionDenied\n\n        return page.make_preview_request(request, preview_mode)\n\n\nclass PreviewOnCreate(PreviewOnEdit):\n    def get_page(self):\n        content_type_app_name = self.kwargs[\"content_type_app_name\"]\n        content_type_model_name = self.kwargs[\"content_type_model_name\"]\n        parent_page_id = self.kwargs[\"parent_page_id\"]\n        try:\n            content_type = ContentType.objects.get_by_natural_key(\n                content_type_app_name, content_type_model_name)\n        except ContentType.DoesNotExist:\n            raise Http404\n\n        page = content_type.model_class()()\n        parent_page = get_object_or_404(Page, id=parent_page_id).specific\n        # We need to populate treebeard's path / depth fields in order to\n        # pass validation. We can't make these 100% consistent with the rest\n        # of the tree without making actual database changes (such as\n        # incrementing the parent's numchild field), but by calling treebeard's\n        # internal _get_path method, we can set a 'realistic' value that will\n        # hopefully enable tree traversal operations\n        # to at least partially work.\n        page.depth = parent_page.depth + 1\n        # Puts the page at the next available path\n        # for a child of `parent_page`.\n        if parent_page.is_leaf():\n            # set the path as the first child of parent_page\n            page.path = page._get_path(parent_page.path, page.depth, 1)\n        else:\n            # add the new page after the last child of parent_page\n            page.path = parent_page.get_last_child()._inc_path()\n\n        return page\n\n    def get_form(self, page, query_dict):\n        form = super().get_form(page, query_dict)\n        if form.is_valid():\n            # Ensures our unsaved page has a suitable url.\n            form.instance.set_url_path(form.parent_page)\n\n            form.instance.full_clean()\n        return form\n", "path": "wagtail/admin/views/pages/preview.py"}]}