We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Only internal redirects
The platform should only redirect to internal pages after logging in.

(handled in `evaluation/views.py index`)


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `evap/evaluation/views.py`
Content:
```
1 from django.contrib import messages
2 from django.contrib.auth import login as auth_login
3 from django.shortcuts import redirect, render
4 from django.utils.translation import ugettext as _
5 
6 from evap.evaluation.forms import NewKeyForm, LoginKeyForm, LoginUsernameForm
7 from evap.evaluation.models import UserProfile, FaqSection, EmailTemplate, Semester
8 
9 
10 def index(request):
11     """Main entry page into EvaP providing all the login options available. THe username/password
12        login is thought to be used for internal users, e.g. by connecting to a LDAP directory.
13        The login key mechanism is meant to be used to include external participants, e.g. visiting
14        students or visiting contributors.
15     """
16 
17     # parse the form data into the respective form
18     submit_type = request.POST.get("submit_type", "no_submit")
19     new_key_form = NewKeyForm(request.POST if submit_type == "new_key" else None)
20     login_key_form = LoginKeyForm(request.POST if submit_type == "login_key" else None)
21     login_username_form = LoginUsernameForm(request, request.POST if submit_type == "login_username" else None)
22 
23     # process form data
24     if request.method == 'POST':
25         if new_key_form.is_valid():
26             # user wants a new login key
27             profile = new_key_form.get_user()
28             profile.generate_login_key()
29             profile.save()
30 
31             EmailTemplate.send_login_key_to_user(new_key_form.get_user())
32 
33             messages.success(request, _("Successfully sent email with new login key."))
34         elif login_key_form.is_valid():
35             # user would like to login with a login key and passed key test
36             auth_login(request, login_key_form.get_user())
37         elif login_username_form.is_valid():
38             # user would like to login with username and password and passed password test
39             auth_login(request, login_username_form.get_user())
40 
41             # clean up our test cookie
42             if request.session.test_cookie_worked():
43                 request.session.delete_test_cookie()
44 
45     # if not logged in by now, render form
46     if not request.user.is_authenticated():
47         # set test cookie to verify whether they work in the next step
48         request.session.set_test_cookie()
49 
50         template_data = dict(new_key_form=new_key_form, login_key_form=login_key_form, login_username_form=login_username_form)
51         return render(request, "index.html", template_data)
52     else:
53         user, created = UserProfile.objects.get_or_create(username=request.user.username)
54 
55         # check for redirect variable
56         redirect_to = request.GET.get("next", None)
57         if redirect_to is not None:
58             if redirect_to.startswith("/staff/"):
59                 if request.user.is_staff:
60                     return redirect(redirect_to)
61             elif redirect_to.startswith("/grades/"):
62                 if request.user.is_grade_publisher:
63                     return redirect(redirect_to)
64             elif redirect_to.startswith("/contributor/"):
65                 if user.is_contributor:
66                     return redirect(redirect_to)
67             elif redirect_to.startswith("/student/"):
68                 if user.is_participant:
69                     return redirect(redirect_to)
70             else:
71                 return redirect(redirect_to)
72 
73         # redirect user to appropriate start page
74         if request.user.is_staff:
75             return redirect('staff:index')
76         elif request.user.is_grade_publisher:
77             return redirect('grades:semester_view', Semester.active_semester().id)
78         elif user.is_contributor_or_delegate:
79             return redirect('contributor:index')
80         elif user.is_participant:
81             return redirect('student:index')
82         else:
83             return redirect('results:index')
84 
85 
86 def faq(request):
87     return render(request, "faq.html", dict(sections=FaqSection.objects.all()))
88 
89 def legal_notice(request):
90     return render(request, "legal_notice.html", dict())
91
```
--- 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/evap/evaluation/views.py b/evap/evaluation/views.py
--- a/evap/evaluation/views.py
+++ b/evap/evaluation/views.py
@@ -2,13 +2,14 @@
 from django.contrib.auth import login as auth_login
 from django.shortcuts import redirect, render
 from django.utils.translation import ugettext as _
+from django.core.urlresolvers import resolve, Resolver404
 
 from evap.evaluation.forms import NewKeyForm, LoginKeyForm, LoginUsernameForm
 from evap.evaluation.models import UserProfile, FaqSection, EmailTemplate, Semester
 
 
 def index(request):
-    """Main entry page into EvaP providing all the login options available. THe username/password
+    """Main entry page into EvaP providing all the login options available. The username/password
        login is thought to be used for internal users, e.g. by connecting to a LDAP directory.
        The login key mechanism is meant to be used to include external participants, e.g. visiting
        students or visiting contributors.
@@ -68,7 +69,12 @@
                 if user.is_participant:
                     return redirect(redirect_to)
             else:
-                return redirect(redirect_to)
+                try:
+                    resolve(redirect_to)
+                except Resolver404:
+                    pass
+                else:
+                    return redirect(redirect_to)
 
         # redirect user to appropriate start page
         if request.user.is_staff:

{"golden_diff": "diff --git a/evap/evaluation/views.py b/evap/evaluation/views.py\n--- a/evap/evaluation/views.py\n+++ b/evap/evaluation/views.py\n@@ -2,13 +2,14 @@\n from django.contrib.auth import login as auth_login\n from django.shortcuts import redirect, render\n from django.utils.translation import ugettext as _\n+from django.core.urlresolvers import resolve, Resolver404\n \n from evap.evaluation.forms import NewKeyForm, LoginKeyForm, LoginUsernameForm\n from evap.evaluation.models import UserProfile, FaqSection, EmailTemplate, Semester\n \n \n def index(request):\n-    \"\"\"Main entry page into EvaP providing all the login options available. THe username/password\n+    \"\"\"Main entry page into EvaP providing all the login options available. The username/password\n        login is thought to be used for internal users, e.g. by connecting to a LDAP directory.\n        The login key mechanism is meant to be used to include external participants, e.g. visiting\n        students or visiting contributors.\n@@ -68,7 +69,12 @@\n                 if user.is_participant:\n                     return redirect(redirect_to)\n             else:\n-                return redirect(redirect_to)\n+                try:\n+                    resolve(redirect_to)\n+                except Resolver404:\n+                    pass\n+                else:\n+                    return redirect(redirect_to)\n \n         # redirect user to appropriate start page\n         if request.user.is_staff:\n", "issue": "Only internal redirects\nThe platform should only redirect to internal pages after logging in.\n\n(handled in `evaluation/views.py index`)\n\n", "before_files": [{"content": "from django.contrib import messages\nfrom django.contrib.auth import login as auth_login\nfrom django.shortcuts import redirect, render\nfrom django.utils.translation import ugettext as _\n\nfrom evap.evaluation.forms import NewKeyForm, LoginKeyForm, LoginUsernameForm\nfrom evap.evaluation.models import UserProfile, FaqSection, EmailTemplate, Semester\n\n\ndef index(request):\n    \"\"\"Main entry page into EvaP providing all the login options available. THe username/password\n       login is thought to be used for internal users, e.g. by connecting to a LDAP directory.\n       The login key mechanism is meant to be used to include external participants, e.g. visiting\n       students or visiting contributors.\n    \"\"\"\n\n    # parse the form data into the respective form\n    submit_type = request.POST.get(\"submit_type\", \"no_submit\")\n    new_key_form = NewKeyForm(request.POST if submit_type == \"new_key\" else None)\n    login_key_form = LoginKeyForm(request.POST if submit_type == \"login_key\" else None)\n    login_username_form = LoginUsernameForm(request, request.POST if submit_type == \"login_username\" else None)\n\n    # process form data\n    if request.method == 'POST':\n        if new_key_form.is_valid():\n            # user wants a new login key\n            profile = new_key_form.get_user()\n            profile.generate_login_key()\n            profile.save()\n\n            EmailTemplate.send_login_key_to_user(new_key_form.get_user())\n\n            messages.success(request, _(\"Successfully sent email with new login key.\"))\n        elif login_key_form.is_valid():\n            # user would like to login with a login key and passed key test\n            auth_login(request, login_key_form.get_user())\n        elif login_username_form.is_valid():\n            # user would like to login with username and password and passed password test\n            auth_login(request, login_username_form.get_user())\n\n            # clean up our test cookie\n            if request.session.test_cookie_worked():\n                request.session.delete_test_cookie()\n\n    # if not logged in by now, render form\n    if not request.user.is_authenticated():\n        # set test cookie to verify whether they work in the next step\n        request.session.set_test_cookie()\n\n        template_data = dict(new_key_form=new_key_form, login_key_form=login_key_form, login_username_form=login_username_form)\n        return render(request, \"index.html\", template_data)\n    else:\n        user, created = UserProfile.objects.get_or_create(username=request.user.username)\n\n        # check for redirect variable\n        redirect_to = request.GET.get(\"next\", None)\n        if redirect_to is not None:\n            if redirect_to.startswith(\"/staff/\"):\n                if request.user.is_staff:\n                    return redirect(redirect_to)\n            elif redirect_to.startswith(\"/grades/\"):\n                if request.user.is_grade_publisher:\n                    return redirect(redirect_to)\n            elif redirect_to.startswith(\"/contributor/\"):\n                if user.is_contributor:\n                    return redirect(redirect_to)\n            elif redirect_to.startswith(\"/student/\"):\n                if user.is_participant:\n                    return redirect(redirect_to)\n            else:\n                return redirect(redirect_to)\n\n        # redirect user to appropriate start page\n        if request.user.is_staff:\n            return redirect('staff:index')\n        elif request.user.is_grade_publisher:\n            return redirect('grades:semester_view', Semester.active_semester().id)\n        elif user.is_contributor_or_delegate:\n            return redirect('contributor:index')\n        elif user.is_participant:\n            return redirect('student:index')\n        else:\n            return redirect('results:index')\n\n\ndef faq(request):\n    return render(request, \"faq.html\", dict(sections=FaqSection.objects.all()))\n\ndef legal_notice(request):\n    return render(request, \"legal_notice.html\", dict())\n", "path": "evap/evaluation/views.py"}], "after_files": [{"content": "from django.contrib import messages\nfrom django.contrib.auth import login as auth_login\nfrom django.shortcuts import redirect, render\nfrom django.utils.translation import ugettext as _\nfrom django.core.urlresolvers import resolve, Resolver404\n\nfrom evap.evaluation.forms import NewKeyForm, LoginKeyForm, LoginUsernameForm\nfrom evap.evaluation.models import UserProfile, FaqSection, EmailTemplate, Semester\n\n\ndef index(request):\n    \"\"\"Main entry page into EvaP providing all the login options available. The username/password\n       login is thought to be used for internal users, e.g. by connecting to a LDAP directory.\n       The login key mechanism is meant to be used to include external participants, e.g. visiting\n       students or visiting contributors.\n    \"\"\"\n\n    # parse the form data into the respective form\n    submit_type = request.POST.get(\"submit_type\", \"no_submit\")\n    new_key_form = NewKeyForm(request.POST if submit_type == \"new_key\" else None)\n    login_key_form = LoginKeyForm(request.POST if submit_type == \"login_key\" else None)\n    login_username_form = LoginUsernameForm(request, request.POST if submit_type == \"login_username\" else None)\n\n    # process form data\n    if request.method == 'POST':\n        if new_key_form.is_valid():\n            # user wants a new login key\n            profile = new_key_form.get_user()\n            profile.generate_login_key()\n            profile.save()\n\n            EmailTemplate.send_login_key_to_user(new_key_form.get_user())\n\n            messages.success(request, _(\"Successfully sent email with new login key.\"))\n        elif login_key_form.is_valid():\n            # user would like to login with a login key and passed key test\n            auth_login(request, login_key_form.get_user())\n        elif login_username_form.is_valid():\n            # user would like to login with username and password and passed password test\n            auth_login(request, login_username_form.get_user())\n\n            # clean up our test cookie\n            if request.session.test_cookie_worked():\n                request.session.delete_test_cookie()\n\n    # if not logged in by now, render form\n    if not request.user.is_authenticated():\n        # set test cookie to verify whether they work in the next step\n        request.session.set_test_cookie()\n\n        template_data = dict(new_key_form=new_key_form, login_key_form=login_key_form, login_username_form=login_username_form)\n        return render(request, \"index.html\", template_data)\n    else:\n        user, created = UserProfile.objects.get_or_create(username=request.user.username)\n\n        # check for redirect variable\n        redirect_to = request.GET.get(\"next\", None)\n        if redirect_to is not None:\n            if redirect_to.startswith(\"/staff/\"):\n                if request.user.is_staff:\n                    return redirect(redirect_to)\n            elif redirect_to.startswith(\"/grades/\"):\n                if request.user.is_grade_publisher:\n                    return redirect(redirect_to)\n            elif redirect_to.startswith(\"/contributor/\"):\n                if user.is_contributor:\n                    return redirect(redirect_to)\n            elif redirect_to.startswith(\"/student/\"):\n                if user.is_participant:\n                    return redirect(redirect_to)\n            else:\n                try:\n                    resolve(redirect_to)\n                except Resolver404:\n                    pass\n                else:\n                    return redirect(redirect_to)\n\n        # redirect user to appropriate start page\n        if request.user.is_staff:\n            return redirect('staff:index')\n        elif request.user.is_grade_publisher:\n            return redirect('grades:semester_view', Semester.active_semester().id)\n        elif user.is_contributor_or_delegate:\n            return redirect('contributor:index')\n        elif user.is_participant:\n            return redirect('student:index')\n        else:\n            return redirect('results:index')\n\n\ndef faq(request):\n    return render(request, \"faq.html\", dict(sections=FaqSection.objects.all()))\n\ndef legal_notice(request):\n    return render(request, \"legal_notice.html\", dict())\n", "path": "evap/evaluation/views.py"}]}