We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Keyerror when sending password reset email
When sending a password reset email, I'm getting an internal error

I'll just share the raven error - hopefully that doesn't review all of the site secrets (probably does)

https://app.getsentry.com/share/issue/37343334302e313233323439393235/


--- 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/wagtailadmin/templatetags/wagtailuserbar.py`
Content:
```
1 from __future__ import absolute_import, unicode_literals
2 
3 from django import template
4 from django.template.loader import render_to_string
5 
6 from wagtail.wagtailadmin.userbar import (
7     AddPageItem, AdminItem, ApproveModerationEditPageItem, EditPageItem, ExplorePageItem,
8     RejectModerationEditPageItem)
9 from wagtail.wagtailcore import hooks
10 from wagtail.wagtailcore.models import PAGE_TEMPLATE_VAR, Page, PageRevision
11 
12 # from django.contrib.auth.decorators import permission_required
13 
14 
15 register = template.Library()
16 
17 
18 def get_page_instance(context):
19     """
20     Given a template context, try and find a Page variable in the common
21     places. Returns None if a page can not be found.
22     """
23     possible_names = [PAGE_TEMPLATE_VAR, 'self']
24     for name in possible_names:
25         if name in context:
26             page = context[name]
27             if isinstance(page, Page):
28                 return page
29 
30 
31 @register.simple_tag(takes_context=True)
32 def wagtailuserbar(context, position='bottom-right'):
33     # Find request object
34     request = context['request']
35 
36 
37     # Don't render if user doesn't have permission to access the admin area
38     if not request.user.has_perm('wagtailadmin.access_admin'):
39         return ''
40 
41     # Only render if the context contains a variable referencing a saved page
42     page = get_page_instance(context)
43     if page is None:
44         return ''
45 
46     # Dont render anything if the page has not been saved - i.e. a preview
47     if page.pk is None:
48         return ''
49 
50     try:
51         revision_id = request.revision_id
52     except AttributeError:
53         revision_id = None
54 
55     if revision_id is None:
56         items = [
57             AdminItem(),
58             ExplorePageItem(Page.objects.get(id=page.id)),
59             EditPageItem(Page.objects.get(id=page.id)),
60             AddPageItem(Page.objects.get(id=page.id)),
61         ]
62     else:
63         items = [
64             AdminItem(),
65             ExplorePageItem(PageRevision.objects.get(id=revision_id).page),
66             EditPageItem(PageRevision.objects.get(id=revision_id).page),
67             AddPageItem(PageRevision.objects.get(id=revision_id).page),
68             ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)),
69             RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)),
70         ]
71 
72     for fn in hooks.get_hooks('construct_wagtail_userbar'):
73         fn(request, items)
74 
75     # Render the items
76     rendered_items = [item.render(request) for item in items]
77 
78     # Remove any unrendered items
79     rendered_items = [item for item in rendered_items if item]
80 
81     # Render the userbar items
82     return render_to_string('wagtailadmin/userbar/base.html', {
83         'request': request,
84         'items': rendered_items,
85         'position': position,
86         'page': page,
87         'revision_id': revision_id
88     })
89
```
--- 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/wagtailadmin/templatetags/wagtailuserbar.py b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py
--- a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py
+++ b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py
@@ -31,8 +31,10 @@
 @register.simple_tag(takes_context=True)
 def wagtailuserbar(context, position='bottom-right'):
     # Find request object
-    request = context['request']
-
+    try:
+        request = context['request']
+    except KeyError:
+        return ''
 
     # Don't render if user doesn't have permission to access the admin area
     if not request.user.has_perm('wagtailadmin.access_admin'):

{"golden_diff": "diff --git a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py\n--- a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py\n+++ b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py\n@@ -31,8 +31,10 @@\n @register.simple_tag(takes_context=True)\n def wagtailuserbar(context, position='bottom-right'):\n     # Find request object\n-    request = context['request']\n-\n+    try:\n+        request = context['request']\n+    except KeyError:\n+        return ''\n \n     # Don't render if user doesn't have permission to access the admin area\n     if not request.user.has_perm('wagtailadmin.access_admin'):\n", "issue": "Keyerror when sending password reset email\nWhen sending a password reset email, I'm getting an internal error\n\nI'll just share the raven error - hopefully that doesn't review all of the site secrets (probably does)\n\nhttps://app.getsentry.com/share/issue/37343334302e313233323439393235/\n\n", "before_files": [{"content": "from __future__ import absolute_import, unicode_literals\n\nfrom django import template\nfrom django.template.loader import render_to_string\n\nfrom wagtail.wagtailadmin.userbar import (\n    AddPageItem, AdminItem, ApproveModerationEditPageItem, EditPageItem, ExplorePageItem,\n    RejectModerationEditPageItem)\nfrom wagtail.wagtailcore import hooks\nfrom wagtail.wagtailcore.models import PAGE_TEMPLATE_VAR, Page, PageRevision\n\n# from django.contrib.auth.decorators import permission_required\n\n\nregister = template.Library()\n\n\ndef get_page_instance(context):\n    \"\"\"\n    Given a template context, try and find a Page variable in the common\n    places. Returns None if a page can not be found.\n    \"\"\"\n    possible_names = [PAGE_TEMPLATE_VAR, 'self']\n    for name in possible_names:\n        if name in context:\n            page = context[name]\n            if isinstance(page, Page):\n                return page\n\n\n@register.simple_tag(takes_context=True)\ndef wagtailuserbar(context, position='bottom-right'):\n    # Find request object\n    request = context['request']\n\n\n    # Don't render if user doesn't have permission to access the admin area\n    if not request.user.has_perm('wagtailadmin.access_admin'):\n        return ''\n\n    # Only render if the context contains a variable referencing a saved page\n    page = get_page_instance(context)\n    if page is None:\n        return ''\n\n    # Dont render anything if the page has not been saved - i.e. a preview\n    if page.pk is None:\n        return ''\n\n    try:\n        revision_id = request.revision_id\n    except AttributeError:\n        revision_id = None\n\n    if revision_id is None:\n        items = [\n            AdminItem(),\n            ExplorePageItem(Page.objects.get(id=page.id)),\n            EditPageItem(Page.objects.get(id=page.id)),\n            AddPageItem(Page.objects.get(id=page.id)),\n        ]\n    else:\n        items = [\n            AdminItem(),\n            ExplorePageItem(PageRevision.objects.get(id=revision_id).page),\n            EditPageItem(PageRevision.objects.get(id=revision_id).page),\n            AddPageItem(PageRevision.objects.get(id=revision_id).page),\n            ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)),\n            RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)),\n        ]\n\n    for fn in hooks.get_hooks('construct_wagtail_userbar'):\n        fn(request, items)\n\n    # Render the items\n    rendered_items = [item.render(request) for item in items]\n\n    # Remove any unrendered items\n    rendered_items = [item for item in rendered_items if item]\n\n    # Render the userbar items\n    return render_to_string('wagtailadmin/userbar/base.html', {\n        'request': request,\n        'items': rendered_items,\n        'position': position,\n        'page': page,\n        'revision_id': revision_id\n    })\n", "path": "wagtail/wagtailadmin/templatetags/wagtailuserbar.py"}], "after_files": [{"content": "from __future__ import absolute_import, unicode_literals\n\nfrom django import template\nfrom django.template.loader import render_to_string\n\nfrom wagtail.wagtailadmin.userbar import (\n    AddPageItem, AdminItem, ApproveModerationEditPageItem, EditPageItem, ExplorePageItem,\n    RejectModerationEditPageItem)\nfrom wagtail.wagtailcore import hooks\nfrom wagtail.wagtailcore.models import PAGE_TEMPLATE_VAR, Page, PageRevision\n\n# from django.contrib.auth.decorators import permission_required\n\n\nregister = template.Library()\n\n\ndef get_page_instance(context):\n    \"\"\"\n    Given a template context, try and find a Page variable in the common\n    places. Returns None if a page can not be found.\n    \"\"\"\n    possible_names = [PAGE_TEMPLATE_VAR, 'self']\n    for name in possible_names:\n        if name in context:\n            page = context[name]\n            if isinstance(page, Page):\n                return page\n\n\n@register.simple_tag(takes_context=True)\ndef wagtailuserbar(context, position='bottom-right'):\n    # Find request object\n    try:\n        request = context['request']\n    except KeyError:\n        return ''\n\n    # Don't render if user doesn't have permission to access the admin area\n    if not request.user.has_perm('wagtailadmin.access_admin'):\n        return ''\n\n    # Only render if the context contains a variable referencing a saved page\n    page = get_page_instance(context)\n    if page is None:\n        return ''\n\n    # Dont render anything if the page has not been saved - i.e. a preview\n    if page.pk is None:\n        return ''\n\n    try:\n        revision_id = request.revision_id\n    except AttributeError:\n        revision_id = None\n\n    if revision_id is None:\n        items = [\n            AdminItem(),\n            ExplorePageItem(Page.objects.get(id=page.id)),\n            EditPageItem(Page.objects.get(id=page.id)),\n            AddPageItem(Page.objects.get(id=page.id)),\n        ]\n    else:\n        items = [\n            AdminItem(),\n            ExplorePageItem(PageRevision.objects.get(id=revision_id).page),\n            EditPageItem(PageRevision.objects.get(id=revision_id).page),\n            AddPageItem(PageRevision.objects.get(id=revision_id).page),\n            ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)),\n            RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)),\n        ]\n\n    for fn in hooks.get_hooks('construct_wagtail_userbar'):\n        fn(request, items)\n\n    # Render the items\n    rendered_items = [item.render(request) for item in items]\n\n    # Remove any unrendered items\n    rendered_items = [item for item in rendered_items if item]\n\n    # Render the userbar items\n    return render_to_string('wagtailadmin/userbar/base.html', {\n        'request': request,\n        'items': rendered_items,\n        'position': position,\n        'page': page,\n        'revision_id': revision_id\n    })\n", "path": "wagtail/wagtailadmin/templatetags/wagtailuserbar.py"}]}