prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Handle routing takeover at client
  Currently, routing is handled by Django. When the user visits the application initially, the routing should be handled by Django, and after the application loads, the routing should be taken over by client.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `mathesar/urls.py`
  Content:
  ```
  1 from django.urls import include, path
  2 from rest_framework_nested import routers
  3 
  4 from mathesar.views import api, frontend
  5 
  6 
  7 router = routers.DefaultRouter()
  8 router.register(r'tables', api.TableViewSet)
  9 router.register(r'schemas', api.SchemaViewSet)
  10 router.register(r'database_keys', api.DatabaseKeyViewSet, basename='database_keys')
  11 
  12 records_router = routers.NestedSimpleRouter(router, r'tables', lookup='table')
  13 records_router.register(r'records', api.RecordViewSet, basename='table-records')
  14 
  15 urlpatterns = [
  16     path('', frontend.index, name="index"),
  17     path(
  18         'tables/<int:pk>/',
  19         frontend.TableDetail.as_view(),
  20         name='frontend-table-detail',
  21     ),
  22     path('api/v0/', include(router.urls)),
  23     path('api/v0/', include(records_router.urls)),
  24 ]
  25
  ```
  
  Path: `mathesar/views/frontend.py`
  Content:
  ```
  1 from django.http import HttpResponseRedirect
  2 from django.shortcuts import render
  3 from django.urls import reverse
  4 from django.views.generic import DetailView
  5 
  6 from mathesar.forms.forms import UploadFileForm
  7 from mathesar.imports.csv import create_table_from_csv
  8 from mathesar.models import Table, Schema
  9 from mathesar.serializers import SchemaSerializer
  10 
  11 
  12 def index(request):
  13     tables = Table.objects.all()
  14     if request.method == "POST":
  15         form = UploadFileForm(request.POST, request.FILES)
  16         if form.is_valid():
  17             table = create_table_from_csv(
  18                 name=form.cleaned_data["table_name"],
  19                 schema=form.cleaned_data["schema_name"],
  20                 database_key=form.cleaned_data["database_key"],
  21                 csv_file=request.FILES["file"]
  22             )
  23             return HttpResponseRedirect(
  24                 reverse("frontend-table-detail", kwargs={"pk": table.id})
  25             )
  26     else:
  27         form = UploadFileForm()
  28     schema_serializer = SchemaSerializer(Schema.objects.all(), many=True, context={'request': request})
  29     return render(
  30         request,
  31         "mathesar/index.html",
  32         {
  33             "form": form,
  34             "tables": sorted(tables, key=lambda x: x.schema.name),
  35             "schema_data": schema_serializer.data
  36         },
  37     )
  38 
  39 
  40 class TableDetail(DetailView):
  41     context_object_name = "table"
  42     queryset = Table.objects.all()
  43
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/mathesar/urls.py b/mathesar/urls.py
  --- a/mathesar/urls.py
  +++ b/mathesar/urls.py
  @@ -14,11 +14,7 @@
   
   urlpatterns = [
       path('', frontend.index, name="index"),
  -    path(
  -        'tables/<int:pk>/',
  -        frontend.TableDetail.as_view(),
  -        name='frontend-table-detail',
  -    ),
  +    path('tables/<int:pk>', frontend.table, name="table"),
       path('api/v0/', include(router.urls)),
       path('api/v0/', include(records_router.urls)),
   ]
  diff --git a/mathesar/views/frontend.py b/mathesar/views/frontend.py
  --- a/mathesar/views/frontend.py
  +++ b/mathesar/views/frontend.py
  @@ -1,16 +1,22 @@
  -from django.http import HttpResponseRedirect
  +from django.http import JsonResponse
   from django.shortcuts import render
  -from django.urls import reverse
  -from django.views.generic import DetailView
   
   from mathesar.forms.forms import UploadFileForm
   from mathesar.imports.csv import create_table_from_csv
   from mathesar.models import Table, Schema
  -from mathesar.serializers import SchemaSerializer
  +from mathesar.serializers import SchemaSerializer, TableSerializer, RecordSerializer
  +from mathesar.database.utils import get_non_default_database_keys
  +
  +
  +def get_common_data(request):
  +    schema_serializer = SchemaSerializer(Schema.objects.all(), many=True, context={'request': request})
  +    return {
  +        "schemas": schema_serializer.data,
  +        "databases": get_non_default_database_keys(),
  +    }
   
   
   def index(request):
  -    tables = Table.objects.all()
       if request.method == "POST":
           form = UploadFileForm(request.POST, request.FILES)
           if form.is_valid():
  @@ -20,23 +26,32 @@
                   database_key=form.cleaned_data["database_key"],
                   csv_file=request.FILES["file"]
               )
  -            return HttpResponseRedirect(
  -                reverse("frontend-table-detail", kwargs={"pk": table.id})
  -            )
  -    else:
  -        form = UploadFileForm()
  -    schema_serializer = SchemaSerializer(Schema.objects.all(), many=True, context={'request': request})
  +            return JsonResponse({"pk": table.id}, status=200)
       return render(
           request,
           "mathesar/index.html",
           {
  -            "form": form,
  -            "tables": sorted(tables, key=lambda x: x.schema.name),
  -            "schema_data": schema_serializer.data
  -        },
  +            "common_data": get_common_data(request),
  +        }
       )
   
   
  -class TableDetail(DetailView):
  -    context_object_name = "table"
  -    queryset = Table.objects.all()
  +def table(request, pk):
  +    try:
  +        table_data = Table.objects.get(pk=pk)
  +        table_serialized = TableSerializer(table_data, context={'request': request}).data
  +        records_serialized = RecordSerializer(table_data.get_records(limit=50, offset=0), many=True, context={'request': request}).data
  +    except Table.DoesNotExist:
  +        table_serialized = {}
  +        records_serialized = []
  +    return render(
  +        request,
  +        "mathesar/index.html",
  +        {
  +            "common_data": get_common_data(request),
  +            "route_specific_data": {
  +                "table-detail": table_serialized,
  +                "table-records": records_serialized
  +            }
  +        }
  +    )
  
verification_info: {"golden_diff": "diff --git a/mathesar/urls.py b/mathesar/urls.py\n--- a/mathesar/urls.py\n+++ b/mathesar/urls.py\n@@ -14,11 +14,7 @@\n \n urlpatterns = [\n     path('', frontend.index, name=\"index\"),\n-    path(\n-        'tables/<int:pk>/',\n-        frontend.TableDetail.as_view(),\n-        name='frontend-table-detail',\n-    ),\n+    path('tables/<int:pk>', frontend.table, name=\"table\"),\n     path('api/v0/', include(router.urls)),\n     path('api/v0/', include(records_router.urls)),\n ]\ndiff --git a/mathesar/views/frontend.py b/mathesar/views/frontend.py\n--- a/mathesar/views/frontend.py\n+++ b/mathesar/views/frontend.py\n@@ -1,16 +1,22 @@\n-from django.http import HttpResponseRedirect\n+from django.http import JsonResponse\n from django.shortcuts import render\n-from django.urls import reverse\n-from django.views.generic import DetailView\n \n from mathesar.forms.forms import UploadFileForm\n from mathesar.imports.csv import create_table_from_csv\n from mathesar.models import Table, Schema\n-from mathesar.serializers import SchemaSerializer\n+from mathesar.serializers import SchemaSerializer, TableSerializer, RecordSerializer\n+from mathesar.database.utils import get_non_default_database_keys\n+\n+\n+def get_common_data(request):\n+    schema_serializer = SchemaSerializer(Schema.objects.all(), many=True, context={'request': request})\n+    return {\n+        \"schemas\": schema_serializer.data,\n+        \"databases\": get_non_default_database_keys(),\n+    }\n \n \n def index(request):\n-    tables = Table.objects.all()\n     if request.method == \"POST\":\n         form = UploadFileForm(request.POST, request.FILES)\n         if form.is_valid():\n@@ -20,23 +26,32 @@\n                 database_key=form.cleaned_data[\"database_key\"],\n                 csv_file=request.FILES[\"file\"]\n             )\n-            return HttpResponseRedirect(\n-                reverse(\"frontend-table-detail\", kwargs={\"pk\": table.id})\n-            )\n-    else:\n-        form = UploadFileForm()\n-    schema_serializer = SchemaSerializer(Schema.objects.all(), many=True, context={'request': request})\n+            return JsonResponse({\"pk\": table.id}, status=200)\n     return render(\n         request,\n         \"mathesar/index.html\",\n         {\n-            \"form\": form,\n-            \"tables\": sorted(tables, key=lambda x: x.schema.name),\n-            \"schema_data\": schema_serializer.data\n-        },\n+            \"common_data\": get_common_data(request),\n+        }\n     )\n \n \n-class TableDetail(DetailView):\n-    context_object_name = \"table\"\n-    queryset = Table.objects.all()\n+def table(request, pk):\n+    try:\n+        table_data = Table.objects.get(pk=pk)\n+        table_serialized = TableSerializer(table_data, context={'request': request}).data\n+        records_serialized = RecordSerializer(table_data.get_records(limit=50, offset=0), many=True, context={'request': request}).data\n+    except Table.DoesNotExist:\n+        table_serialized = {}\n+        records_serialized = []\n+    return render(\n+        request,\n+        \"mathesar/index.html\",\n+        {\n+            \"common_data\": get_common_data(request),\n+            \"route_specific_data\": {\n+                \"table-detail\": table_serialized,\n+                \"table-records\": records_serialized\n+            }\n+        }\n+    )\n", "issue": "Handle routing takeover at client\nCurrently, routing is handled by Django. When the user visits the application initially, the routing should be handled by Django, and after the application loads, the routing should be taken over by client.\n", "before_files": [{"content": "from django.urls import include, path\nfrom rest_framework_nested import routers\n\nfrom mathesar.views import api, frontend\n\n\nrouter = routers.DefaultRouter()\nrouter.register(r'tables', api.TableViewSet)\nrouter.register(r'schemas', api.SchemaViewSet)\nrouter.register(r'database_keys', api.DatabaseKeyViewSet, basename='database_keys')\n\nrecords_router = routers.NestedSimpleRouter(router, r'tables', lookup='table')\nrecords_router.register(r'records', api.RecordViewSet, basename='table-records')\n\nurlpatterns = [\n    path('', frontend.index, name=\"index\"),\n    path(\n        'tables/<int:pk>/',\n        frontend.TableDetail.as_view(),\n        name='frontend-table-detail',\n    ),\n    path('api/v0/', include(router.urls)),\n    path('api/v0/', include(records_router.urls)),\n]\n", "path": "mathesar/urls.py"}, {"content": "from django.http import HttpResponseRedirect\nfrom django.shortcuts import render\nfrom django.urls import reverse\nfrom django.views.generic import DetailView\n\nfrom mathesar.forms.forms import UploadFileForm\nfrom mathesar.imports.csv import create_table_from_csv\nfrom mathesar.models import Table, Schema\nfrom mathesar.serializers import SchemaSerializer\n\n\ndef index(request):\n    tables = Table.objects.all()\n    if request.method == \"POST\":\n        form = UploadFileForm(request.POST, request.FILES)\n        if form.is_valid():\n            table = create_table_from_csv(\n                name=form.cleaned_data[\"table_name\"],\n                schema=form.cleaned_data[\"schema_name\"],\n                database_key=form.cleaned_data[\"database_key\"],\n                csv_file=request.FILES[\"file\"]\n            )\n            return HttpResponseRedirect(\n                reverse(\"frontend-table-detail\", kwargs={\"pk\": table.id})\n            )\n    else:\n        form = UploadFileForm()\n    schema_serializer = SchemaSerializer(Schema.objects.all(), many=True, context={'request': request})\n    return render(\n        request,\n        \"mathesar/index.html\",\n        {\n            \"form\": form,\n            \"tables\": sorted(tables, key=lambda x: x.schema.name),\n            \"schema_data\": schema_serializer.data\n        },\n    )\n\n\nclass TableDetail(DetailView):\n    context_object_name = \"table\"\n    queryset = Table.objects.all()\n", "path": "mathesar/views/frontend.py"}], "after_files": [{"content": "from django.urls import include, path\nfrom rest_framework_nested import routers\n\nfrom mathesar.views import api, frontend\n\n\nrouter = routers.DefaultRouter()\nrouter.register(r'tables', api.TableViewSet)\nrouter.register(r'schemas', api.SchemaViewSet)\nrouter.register(r'database_keys', api.DatabaseKeyViewSet, basename='database_keys')\n\nrecords_router = routers.NestedSimpleRouter(router, r'tables', lookup='table')\nrecords_router.register(r'records', api.RecordViewSet, basename='table-records')\n\nurlpatterns = [\n    path('', frontend.index, name=\"index\"),\n    path('tables/<int:pk>', frontend.table, name=\"table\"),\n    path('api/v0/', include(router.urls)),\n    path('api/v0/', include(records_router.urls)),\n]\n", "path": "mathesar/urls.py"}, {"content": "from django.http import JsonResponse\nfrom django.shortcuts import render\n\nfrom mathesar.forms.forms import UploadFileForm\nfrom mathesar.imports.csv import create_table_from_csv\nfrom mathesar.models import Table, Schema\nfrom mathesar.serializers import SchemaSerializer, TableSerializer, RecordSerializer\nfrom mathesar.database.utils import get_non_default_database_keys\n\n\ndef get_common_data(request):\n    schema_serializer = SchemaSerializer(Schema.objects.all(), many=True, context={'request': request})\n    return {\n        \"schemas\": schema_serializer.data,\n        \"databases\": get_non_default_database_keys(),\n    }\n\n\ndef index(request):\n    if request.method == \"POST\":\n        form = UploadFileForm(request.POST, request.FILES)\n        if form.is_valid():\n            table = create_table_from_csv(\n                name=form.cleaned_data[\"table_name\"],\n                schema=form.cleaned_data[\"schema_name\"],\n                database_key=form.cleaned_data[\"database_key\"],\n                csv_file=request.FILES[\"file\"]\n            )\n            return JsonResponse({\"pk\": table.id}, status=200)\n    return render(\n        request,\n        \"mathesar/index.html\",\n        {\n            \"common_data\": get_common_data(request),\n        }\n    )\n\n\ndef table(request, pk):\n    try:\n        table_data = Table.objects.get(pk=pk)\n        table_serialized = TableSerializer(table_data, context={'request': request}).data\n        records_serialized = RecordSerializer(table_data.get_records(limit=50, offset=0), many=True, context={'request': request}).data\n    except Table.DoesNotExist:\n        table_serialized = {}\n        records_serialized = []\n    return render(\n        request,\n        \"mathesar/index.html\",\n        {\n            \"common_data\": get_common_data(request),\n            \"route_specific_data\": {\n                \"table-detail\": table_serialized,\n                \"table-records\": records_serialized\n            }\n        }\n    )\n", "path": "mathesar/views/frontend.py"}]}