Repository: django-hijack/django-hijack

Issue:
Context processor data not available in notification
The notification is [rendered without any context](https://github.com/django-hijack/django-hijack/blob/ae1d9969346d885570276c3ba1ac307085ac1ed6/hijack/middleware.py#L45), so the context I'm expecting from my context processors doesn't show.

Could the render be changed to use a context?
Just by doing this, I believe the context processors should add their data.


Discussion:
Hello again @gdvalderrama,

Thanks for your continuous curiosity about this project. Let's see first if I correctly understood your question, OK?

I presume you want to render some special bits into the template that you inject either via the request or through context processors, right?

We currently pass a context to the template renderer. However, we don't pass a request to the `render_to_string` function. This means it will only create a simple `RenderContext` and not a `RequestContext`. Only the latter will run the context processors.

I don't see I reason why we shouldn't change that behavior. In fact, it will simplify the middleware.

I will create a pull-request. I'd appreciate if you review it and let me know if that solves the issue.

Cheers!
Joe



Resolving patch:
diff --git a/hijack/contrib/admin/admin.py b/hijack/contrib/admin/admin.py
--- a/hijack/contrib/admin/admin.py
+++ b/hijack/contrib/admin/admin.py
@@ -52,6 +52,7 @@ def hijack_button(self, request, obj):
                 "is_user_admin": self.model == type(user),
                 "next": self.get_hijack_success_url(request, obj),
             },
+            request=request,
         )
 
     def get_changelist_instance(self, request):
diff --git a/hijack/middleware.py b/hijack/middleware.py
--- a/hijack/middleware.py
+++ b/hijack/middleware.py
@@ -44,7 +44,8 @@ def process_response(self, request, response):
 
         rendered = render_to_string(
             "hijack/notification.html",
-            {"request": request, "csrf_token": request.META["CSRF_COOKIE"]},
+            {"request": request},
+            request=request,
         )
 
         # Insert the toolbar in the response.


Test patch:
diff --git a/hijack/tests/test_middleware.py b/hijack/tests/test_middleware.py
--- a/hijack/tests/test_middleware.py
+++ b/hijack/tests/test_middleware.py
@@ -71,7 +71,8 @@ def test_process_response__html(self, rf, monkeypatch):
         assert self.middleware.process_response(request, response) is response
         render_to_string.assert_called_once_with(
             "hijack/notification.html",
-            {"request": request, "csrf_token": "123456"},
+            {"request": request},
+            request=request,
         )
 
     def test_process_response__non_html(self, rf, monkeypatch):
@@ -99,7 +100,8 @@ def test_process_response__content_length(self, rf, monkeypatch):
         assert self.middleware.process_response(request, response) is response
         render_to_string.assert_called_once_with(
             "hijack/notification.html",
-            {"request": request, "csrf_token": "123456"},
+            {"request": request},
+            request=request,
         )
         assert response["Content-Length"] == "21"
         assert response.content == b"<body>HIJACKED</body>"