Skip to content

Commit 51adb6b

Browse files
authored
Don't use exception, instead of figure out what view it is. (#124)
Figuring out which view it is and adding the correct property to that view is better than trying to re-render the view itself. This should solve for django's generic views. It won't work for Django vanilla views package, but we can deal with this later if someone show interest for that.
1 parent 09375f9 commit 51adb6b

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

sockpuppet/reflex.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ def get_context_data(self, *args, **kwargs):
3434
resolved = resolve(parsed_url.path)
3535
view = resolved.func.view_class()
3636
view.request = self.request
37-
try:
38-
view.kwargs = resolved.kwargs
39-
context = view.get_context_data(**{"stimulus_reflex": True})
40-
except AttributeError:
41-
view.get(self.request)
42-
context = view.get_context_data(**{"stimulus_reflex": True})
37+
view.kwargs = resolved.kwargs
38+
39+
# correct for detail and list views for django generic views
40+
if hasattr(view, "get_object"):
41+
view.object = view.get_object()
42+
43+
if hasattr(view, "paginate_queryset"):
44+
view.object_list = view.get_queryset()
45+
46+
context = view.get_context_data(**{"stimulus_reflex": True})
4347

4448
self.context = context
4549
self.context.update(**kwargs)

0 commit comments

Comments
 (0)