Commits

Simon Meers committed 9ff5f96

Opt into ajax_catch_redirects and add some missing imports.

Comments (0)

Files changed (1)

generic/views/mixins.py

+import json
 import django.views.generic
+from django import http
 from django.contrib.auth.decorators import login_required
 from django.utils.decorators import method_decorator
 from .exceptions import PermissionDenied, RedirectInstead
 
 class View(django.views.generic.View):
     result_text = 'OK'
+    ajax_catch_redirects = False
     default_context_data = {}
 
     def __init__(self, *args, **kwargs):
     def finalize_response(self, response):
         """ Hook for any last-minute response tweaking; e.g. JSON for AJAX """
         if self.request.is_ajax() and response.status_code == 302:
-            return http.HttpResponse(
-                simplejson.dumps(
-                    {
-                        'redirect': response['location'],
-                        'result': self.result_text,
-                    }
-                ),
-                mimetype="application/json"
-            )
+            if self.ajax_catch_redirects:
+                return http.HttpResponse(
+                    json.dumps(
+                        {
+                            'redirect': response['location'],
+                            'result': self.result_text,
+                        }
+                    ),
+                    mimetype="application/json"
+                )
         return response
 
     def dispatch(self, request, *args, **kwargs):