#17734: 302 redirect to standard language for non-i18n URL's when returning 404.
-------------------------------------+-------------------------------------
     Reporter:  petar@…              |                    Owner:  Jannis
                                     |  Leidel
         Type:  Bug                  |                   Status:  closed
    Component:                       |                  Version:  master
  Internationalization               |
     Severity:  Release blocker      |               Resolution:  fixed
     Keywords:  urlconf, i18n        |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by gitaarik):

 For what it's worth (this is a old ticket), I ran into the same problem as
 @geoffhing, where I had a API which on 404 would redirect to a language-
 prefixed URL because I was using a catch-all url pattern:


 {{{
 urlpatterns = [
     url(r'^api/', include(api.urls)),
 ]

 urlpatterns += i18n_patterns(
     url(r'^something/', include('something.urls')),
     url(r'^', include(wagtail_urls)),
 )
 }}}

 I fixed it by extending the default Django LocaleMiddleware:

 {{{
 from django.middleware.locale import LocaleMiddleware as
 DjangoLocaleMiddleware


 class LocaleMiddleware(DjangoLocaleMiddleware):

     def process_response(self, request, response):

         if request.path.startswith('/api/'):
             # Prevent `/api/` URLs from redirecting to language-prefixed
 URLs,
             # because the API client will get a 302 redirect response
 instead
             # of 404, which is not what we want.
             return response

         return super().process_response(request, response)
 }}}

 This was the best solution I could come up with. I preferrably don't
 override default django classes but in this case I couldn't think of any
 other solution. It works quite fine though.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/17734#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/073.54b3faa3e18b0e13dc5aff72bb452c08%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to