We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
500 on bad certificate number
# Problem

When I try and add a bad certificate number, then I get a 500, I should get a 404.
See:
http://staging.changelog.qgis.org/en/qgis/certificate/0246242/

# Proposed Solution

Return a 404

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `django_project/certification/views/certificate.py`
Content:
```
1 # coding=utf-8
2 from django.http import Http404
3 from django.views.generic import CreateView, DetailView
4 from django.core.urlresolvers import reverse
5 from braces.views import LoginRequiredMixin
6 from ..models import Certificate, Course, Attendee
7 from ..forms import CertificateForm
8 
9 
10 class CertificateMixin(object):
11     """Mixin class to provide standard settings for Certificate."""
12 
13     model = Certificate
14     form_class = CertificateForm
15 
16 
17 class CertificateCreateView(
18         LoginRequiredMixin, CertificateMixin, CreateView):
19     """Create view for Certificate."""
20 
21     context_object_name = 'certificate'
22     template_name = 'certificate/create.html'
23 
24     def get_success_url(self):
25         """Define the redirect URL.
26 
27         After successful creation of the object, the User will be redirected
28         to the Course detail page.
29 
30        :returns: URL
31        :rtype: HttpResponse
32        """
33 
34         return reverse('course-detail', kwargs={
35             'project_slug': self.project_slug,
36             'organisation_slug': self.organisation_slug,
37             'slug': self.course_slug
38         })
39 
40     def get_context_data(self, **kwargs):
41         """Get the context data which is passed to a template.
42 
43         :param kwargs: Any arguments to pass to the superclass.
44         :type kwargs: dict
45 
46         :returns: Context data which will be passed to the template.
47         :rtype: dict
48         """
49 
50         context = super(
51             CertificateCreateView, self).get_context_data(**kwargs)
52         context['course'] = Course.objects.get(slug=self.course_slug)
53         context['attendee'] = Attendee.objects.get(pk=self.pk)
54         return context
55 
56     def get_form_kwargs(self):
57         """Get keyword arguments from form.
58 
59         :returns keyword argument from the form
60         :rtype: dict
61         """
62 
63         kwargs = super(CertificateCreateView, self).get_form_kwargs()
64         self.project_slug = self.kwargs.get('project_slug', None)
65         self.organisation_slug = self.kwargs.get('organisation_slug', None)
66         self.course_slug = self.kwargs.get('course_slug', None)
67         self.pk = self.kwargs.get('pk', None)
68         self.course = Course.objects.get(slug=self.course_slug)
69         self.attendee = Attendee.objects.get(pk=self.pk)
70         kwargs.update({
71             'user': self.request.user,
72             'course': self.course,
73             'attendee': self.attendee,
74         })
75         return kwargs
76 
77 
78 class CertificateDetailView(DetailView):
79     """Detail view for Certificate."""
80 
81     model = Certificate
82     context_object_name = 'certificate'
83     template_name = 'certificate/detail.html'
84 
85     def get_context_data(self, **kwargs):
86         """Get the context data which is passed to a template.
87 
88         :param kwargs: Any arguments to pass to the superclass.
89         :type kwargs: dict
90 
91         :returns: Context data which will be passed to the template.
92         :rtype: dict
93         """
94 
95         self.certificateID = self.kwargs.get('id', None)
96         context = super(
97             CertificateDetailView, self).get_context_data(**kwargs)
98         context['certificate'] = \
99             Certificate.objects.get(certificateID=self.certificateID)
100         return context
101 
102     def get_queryset(self):
103         """Get the queryset for this view.
104 
105         :returns: Queryset which is all certificate in the
106             corresponding organisation.
107         :rtype: QuerySet
108         """
109 
110         qs = Certificate.objects.all()
111         return qs
112 
113     def get_object(self, queryset=None):
114         """Get the object for this view.
115 
116         :param queryset: A query set
117         :type queryset: QuerySet
118 
119         :returns: Queryset which is filtered to only show a certificate
120             depends on the input certificate ID.
121         :rtype: QuerySet
122         :raises: Http404
123         """
124 
125         if queryset is None:
126             queryset = self.get_queryset()
127             certificateID = self.kwargs.get('id', None)
128             if certificateID:
129                 obj = queryset.get(
130                     certificateID=certificateID)
131                 return obj
132             else:
133                 raise Http404('Sorry! Certificate by this ID is not exist.')
134
```
--- 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/django_project/certification/views/certificate.py b/django_project/certification/views/certificate.py
--- a/django_project/certification/views/certificate.py
+++ b/django_project/certification/views/certificate.py
@@ -93,10 +93,15 @@
         """
 
         self.certificateID = self.kwargs.get('id', None)
+        self.project_slug = self.kwargs.get('project_slug', None)
         context = super(
             CertificateDetailView, self).get_context_data(**kwargs)
-        context['certificate'] = \
-            Certificate.objects.get(certificateID=self.certificateID)
+        issued_id = \
+            Certificate.objects.all().values_list('certificateID', flat=True)
+        if self.certificateID in issued_id:
+            context['certificate'] = \
+                Certificate.objects.get(certificateID=self.certificateID)
+        context['project_slug'] = self.project_slug
         return context
 
     def get_queryset(self):
@@ -126,8 +131,10 @@
             queryset = self.get_queryset()
             certificateID = self.kwargs.get('id', None)
             if certificateID:
-                obj = queryset.get(
-                    certificateID=certificateID)
-                return obj
+                try:
+                    obj = queryset.get(certificateID=certificateID)
+                    return obj
+                except Certificate.DoesNotExist:
+                    return None
             else:
                 raise Http404('Sorry! Certificate by this ID is not exist.')

{"golden_diff": "diff --git a/django_project/certification/views/certificate.py b/django_project/certification/views/certificate.py\n--- a/django_project/certification/views/certificate.py\n+++ b/django_project/certification/views/certificate.py\n@@ -93,10 +93,15 @@\n         \"\"\"\n \n         self.certificateID = self.kwargs.get('id', None)\n+        self.project_slug = self.kwargs.get('project_slug', None)\n         context = super(\n             CertificateDetailView, self).get_context_data(**kwargs)\n-        context['certificate'] = \\\n-            Certificate.objects.get(certificateID=self.certificateID)\n+        issued_id = \\\n+            Certificate.objects.all().values_list('certificateID', flat=True)\n+        if self.certificateID in issued_id:\n+            context['certificate'] = \\\n+                Certificate.objects.get(certificateID=self.certificateID)\n+        context['project_slug'] = self.project_slug\n         return context\n \n     def get_queryset(self):\n@@ -126,8 +131,10 @@\n             queryset = self.get_queryset()\n             certificateID = self.kwargs.get('id', None)\n             if certificateID:\n-                obj = queryset.get(\n-                    certificateID=certificateID)\n-                return obj\n+                try:\n+                    obj = queryset.get(certificateID=certificateID)\n+                    return obj\n+                except Certificate.DoesNotExist:\n+                    return None\n             else:\n                 raise Http404('Sorry! Certificate by this ID is not exist.')\n", "issue": "500 on bad certificate number\n# Problem\r\n\r\nWhen I try and add a bad certificate number, then I get a 500, I should get a 404.\r\nSee:\r\nhttp://staging.changelog.qgis.org/en/qgis/certificate/0246242/\r\n\r\n# Proposed Solution\r\n\r\nReturn a 404\n", "before_files": [{"content": "# coding=utf-8\nfrom django.http import Http404\nfrom django.views.generic import CreateView, DetailView\nfrom django.core.urlresolvers import reverse\nfrom braces.views import LoginRequiredMixin\nfrom ..models import Certificate, Course, Attendee\nfrom ..forms import CertificateForm\n\n\nclass CertificateMixin(object):\n    \"\"\"Mixin class to provide standard settings for Certificate.\"\"\"\n\n    model = Certificate\n    form_class = CertificateForm\n\n\nclass CertificateCreateView(\n        LoginRequiredMixin, CertificateMixin, CreateView):\n    \"\"\"Create view for Certificate.\"\"\"\n\n    context_object_name = 'certificate'\n    template_name = 'certificate/create.html'\n\n    def get_success_url(self):\n        \"\"\"Define the redirect URL.\n\n        After successful creation of the object, the User will be redirected\n        to the Course detail page.\n\n       :returns: URL\n       :rtype: HttpResponse\n       \"\"\"\n\n        return reverse('course-detail', kwargs={\n            'project_slug': self.project_slug,\n            'organisation_slug': self.organisation_slug,\n            'slug': self.course_slug\n        })\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(\n            CertificateCreateView, self).get_context_data(**kwargs)\n        context['course'] = Course.objects.get(slug=self.course_slug)\n        context['attendee'] = Attendee.objects.get(pk=self.pk)\n        return context\n\n    def get_form_kwargs(self):\n        \"\"\"Get keyword arguments from form.\n\n        :returns keyword argument from the form\n        :rtype: dict\n        \"\"\"\n\n        kwargs = super(CertificateCreateView, self).get_form_kwargs()\n        self.project_slug = self.kwargs.get('project_slug', None)\n        self.organisation_slug = self.kwargs.get('organisation_slug', None)\n        self.course_slug = self.kwargs.get('course_slug', None)\n        self.pk = self.kwargs.get('pk', None)\n        self.course = Course.objects.get(slug=self.course_slug)\n        self.attendee = Attendee.objects.get(pk=self.pk)\n        kwargs.update({\n            'user': self.request.user,\n            'course': self.course,\n            'attendee': self.attendee,\n        })\n        return kwargs\n\n\nclass CertificateDetailView(DetailView):\n    \"\"\"Detail view for Certificate.\"\"\"\n\n    model = Certificate\n    context_object_name = 'certificate'\n    template_name = 'certificate/detail.html'\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        self.certificateID = self.kwargs.get('id', None)\n        context = super(\n            CertificateDetailView, self).get_context_data(**kwargs)\n        context['certificate'] = \\\n            Certificate.objects.get(certificateID=self.certificateID)\n        return context\n\n    def get_queryset(self):\n        \"\"\"Get the queryset for this view.\n\n        :returns: Queryset which is all certificate in the\n            corresponding organisation.\n        :rtype: QuerySet\n        \"\"\"\n\n        qs = Certificate.objects.all()\n        return qs\n\n    def get_object(self, queryset=None):\n        \"\"\"Get the object for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: Queryset which is filtered to only show a certificate\n            depends on the input certificate ID.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if queryset is None:\n            queryset = self.get_queryset()\n            certificateID = self.kwargs.get('id', None)\n            if certificateID:\n                obj = queryset.get(\n                    certificateID=certificateID)\n                return obj\n            else:\n                raise Http404('Sorry! Certificate by this ID is not exist.')\n", "path": "django_project/certification/views/certificate.py"}], "after_files": [{"content": "# coding=utf-8\nfrom django.http import Http404\nfrom django.views.generic import CreateView, DetailView\nfrom django.core.urlresolvers import reverse\nfrom braces.views import LoginRequiredMixin\nfrom ..models import Certificate, Course, Attendee\nfrom ..forms import CertificateForm\n\n\nclass CertificateMixin(object):\n    \"\"\"Mixin class to provide standard settings for Certificate.\"\"\"\n\n    model = Certificate\n    form_class = CertificateForm\n\n\nclass CertificateCreateView(\n        LoginRequiredMixin, CertificateMixin, CreateView):\n    \"\"\"Create view for Certificate.\"\"\"\n\n    context_object_name = 'certificate'\n    template_name = 'certificate/create.html'\n\n    def get_success_url(self):\n        \"\"\"Define the redirect URL.\n\n        After successful creation of the object, the User will be redirected\n        to the Course detail page.\n\n       :returns: URL\n       :rtype: HttpResponse\n       \"\"\"\n\n        return reverse('course-detail', kwargs={\n            'project_slug': self.project_slug,\n            'organisation_slug': self.organisation_slug,\n            'slug': self.course_slug\n        })\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(\n            CertificateCreateView, self).get_context_data(**kwargs)\n        context['course'] = Course.objects.get(slug=self.course_slug)\n        context['attendee'] = Attendee.objects.get(pk=self.pk)\n        return context\n\n    def get_form_kwargs(self):\n        \"\"\"Get keyword arguments from form.\n\n        :returns keyword argument from the form\n        :rtype: dict\n        \"\"\"\n\n        kwargs = super(CertificateCreateView, self).get_form_kwargs()\n        self.project_slug = self.kwargs.get('project_slug', None)\n        self.organisation_slug = self.kwargs.get('organisation_slug', None)\n        self.course_slug = self.kwargs.get('course_slug', None)\n        self.pk = self.kwargs.get('pk', None)\n        self.course = Course.objects.get(slug=self.course_slug)\n        self.attendee = Attendee.objects.get(pk=self.pk)\n        kwargs.update({\n            'user': self.request.user,\n            'course': self.course,\n            'attendee': self.attendee,\n        })\n        return kwargs\n\n\nclass CertificateDetailView(DetailView):\n    \"\"\"Detail view for Certificate.\"\"\"\n\n    model = Certificate\n    context_object_name = 'certificate'\n    template_name = 'certificate/detail.html'\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        self.certificateID = self.kwargs.get('id', None)\n        self.project_slug = self.kwargs.get('project_slug', None)\n        context = super(\n            CertificateDetailView, self).get_context_data(**kwargs)\n        issued_id = \\\n            Certificate.objects.all().values_list('certificateID', flat=True)\n        if self.certificateID in issued_id:\n            context['certificate'] = \\\n                Certificate.objects.get(certificateID=self.certificateID)\n        context['project_slug'] = self.project_slug\n        return context\n\n    def get_queryset(self):\n        \"\"\"Get the queryset for this view.\n\n        :returns: Queryset which is all certificate in the\n            corresponding organisation.\n        :rtype: QuerySet\n        \"\"\"\n\n        qs = Certificate.objects.all()\n        return qs\n\n    def get_object(self, queryset=None):\n        \"\"\"Get the object for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: Queryset which is filtered to only show a certificate\n            depends on the input certificate ID.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if queryset is None:\n            queryset = self.get_queryset()\n            certificateID = self.kwargs.get('id', None)\n            if certificateID:\n                try:\n                    obj = queryset.get(certificateID=certificateID)\n                    return obj\n                except Certificate.DoesNotExist:\n                    return None\n            else:\n                raise Http404('Sorry! Certificate by this ID is not exist.')\n", "path": "django_project/certification/views/certificate.py"}]}