Reputation: 797
I'm new to Django and working on a project here at work where there is that little problem I can't seem to find a fix for:
I got my project accessible by using localhost:8000/
. This URL redirects to localhost:8000/en/dimension/
. /en/
being the default locale of my browser so it's all good.
But the problem is, localhost:8000/fr/
still redirects to /en/dimension/
and same goes for /nl/
.
Here is my urls.py file:
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.shortcuts import redirect
from django.contrib import admin
from opendataApp.views import dimensionStep, productStep, zoneStep, formatStep, geographicalAreaStep, customStep, downloadStep, proxy, downloadCustom, statistics, statisticsReset, statisticsFilter
admin.autodiscover()
urlpatterns = patterns(
'',
url(r'^proxy/urbis/(?P<path>.*)$', proxy),
#translation
(r'^i18n/', include('django.conf.urls.i18n')),
(r'^admin/', include(admin.site.urls)),
# Uncomment the admin/doc line below to enable admin documentation:
#url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns(
'',
url(r'^$', lambda r: redirect('/dimension/'), name="home"),
url(r'^dimension', dimensionStep, name="dimensionStep"),
url(r'^product', productStep, name="productStep"),
url(r'^zone', zoneStep, name="zoneStep"),
url(r'^format', formatStep, name="formatStep"),
url(r'^selection', geographicalAreaStep, name="geographicalAreaStep"),
url(r'^custom-selection', customStep, name="custom"),
url(r'^download/$', downloadStep, name="download"),
url(r'^download-custom/$', downloadCustom, name="downloadCustom"),
url(r'^statistics/$', statistics, name="statistics"),
url(r'^statistics/reset$', statisticsReset, name="statisticsReset"),
url(r'^statistics/filter$', statisticsFilter, name="statisticsFilter"),
url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
)
So my main issue is that redirect('/dimension/')
ignores the previously asked locale and falls back to default locale again. Any idea how to fix this?
Thanks!
Upvotes: 0
Views: 599
Reputation: 1123560
The LocaleMiddleware
is responsible for determining what language the browser should be served; it is consistently picking en
for your browser, and you need to figure out why.
See How Django discovers language preference for the details, but the short order is:
/
and /dimension/
there isn't one yet._language
key in the session (falling back to django_language
for backwards compatibility).django_language
cookie (or whatever you set in settings.LANGUAGE_COOKIE_NAME
)Accept-Language
headersettings.LANGUAGE_CODE
.Note that nothing sets the session key or cookie explicitly; your own code needs to do so. You can use the set_language()
redirect view to do this for you; a form in your web UI would POST to that view to let a visitor set a different language.
You can also set the session key or cookie explicitly when a visitor comes to /fr/
and doesn't yet have an explicit cookie or session key. You'd do so in a view attached to ^$
in the i18n_patterns()
url map (which is called for any /<language_code>/
request), instead of the redirect you have now. That view can still redirect, of course.
Even without explicitly setting the cookie, your redirect should at the very least use the request.LANGUAGE_CODE
variable:
url(r'^$', lambda r: redirect('/{}/dimension/'.format(r.LANGUAGE_CODE)), name="home"),
Upvotes: 2
Reputation: 665
Have you tried clearing your browser cache? Browsers often cache redirects.
Upvotes: 1