Commits

Carl Meyer committed c8ecea9

Bikeshed the setting name again, and add docs.

  • Participants
  • Parent commits 66ebe08

Comments (0)

Files changed (4)

   an intermediate fallback in case no locale is specified in the
   URL. (The default behavior, preserved for backwards compatibility,
   is to fallback directly to ``settings.LANGUAGE_CODE``).
+
+``LOCALE_REDIRECT_PERMANENT`` (default: ``True``)
+  Whether to use a permanent redirect (301 status code) or temporary
+  redirect (302) when redirecting users from the no-locale version of a URL
+  to the default locale (or the locale specified in their Accept-Language
+  header if ``LOCALEURL_USE_ACCEPT_LANGUAGE`` is True).  If Accept-Language
+  is not used, these redirects will never change (as long as the default
+  locale never changes), so 301 (the default) is a fine choice.  If you use
+  Accept-Language you may want to consider switching this to ``False``, as
+  the redirect will then be dependent on the user's Accept-Language header.

localeurl/middleware.py

                 locale_path = "%s?%s" % (locale_path,
                         request.META['QUERY_STRING'])
             locale_url = utils.add_script_prefix(locale_path)
-            redirect_class = {
-                301: HttpResponsePermanentRedirect,
-                302: HttpResponseRedirect,
-            }.get(getattr(localeurl_settings, "LOCALE_REDIRECT_CODE"),
-                  HttpResponsePermanentRedirect)
+            redirect_class = HttpResponsePermanentRedirect
+            if not localeurl_settings.LOCALE_REDIRECT_PERMANENT:
+                redirect_class = HttpResponseRedirect
             # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect
             return redirect_class(iri_to_uri(locale_url))
         request.path_info = path

localeurl/settings.py

 
 USE_ACCEPT_LANGUAGE = getattr(settings, 'LOCALEURL_USE_ACCEPT_LANGUAGE', False)
 
-LOCALE_REDIRECT_CODE = getattr(settings, 'LOCALE_REDIRECT_CODE', 301)
+LOCALE_REDIRECT_PERMANENT = getattr(settings, 'LOCALE_REDIRECT_PERMANENT', True)

localeurl/tests/tests.py

 
 
     def test_redirect_statuscode_302(self):
-        self.settings_manager.set(LOCALE_REDIRECT_CODE=302)
+        self.settings_manager.set(LOCALE_REDIRECT_PERMANENT=False)
         reload(localeurl_settings)
 
         r1 = self.request_factory.get('/nl-be/test/independent/?foo=bar')
         self.assertEqual(302, r2.status_code)
 
 
-    def test_redirect_statuscode_not_supported(self):
-        self.settings_manager.set(LOCALE_REDIRECT_CODE=418)
-        reload(localeurl_settings)
-
+    def test_redirect_statuscode_default(self):
         r1 = self.request_factory.get('/nl-be/test/independent/?foo=bar')
         r2 = self.middleware.process_request(r1)
         self.assertEqual(301, r2.status_code)