'new locale not redirecting properly
I have a basic django cms running with lots of localised content. I have been assigned to add few more languages before content can take over.
here is a sample english url:
<domain>/en/myhelp/
this is the sample menu which is supposed to render the menu dropdown:
<body>
{% cms_toolbar %}
<div class="container faq-main main-block">
<div class="select-wrapper">
<select name="language" id="faq-lang" class="lang-btn hide">
<option value="en">English</option>
<option value="hi">हिंदी</option>
<option value="mr">मराठी</option>
<option value="te">తెలుగు</option>
<option value="ta">தமிழ்</option>
<option value="kn">ಕನ್ನಡ</option>
<option value="bn">বাংলা</option>
<option value="ml">മലയാള൦</option>
<option value="gu">ગુજરાતી</option>
</select>
</div>
{% block content %}{% endblock content %}
{% block contact %}{% endblock contact %}
{% block actions %}{% endblock actions %}
</div>
{% render_bundle 'faq' 'js' %}
{% render_block "js" %}
</body>
any language selected from menu dropdown updates the url accordingly. for example on selecting mr, url mentioned above would change to:
<domain>/mr/myhelp/
All good so far.
Now i have added 2 more langs in this menu:
<option value="od">ଓଡିଆ</option>
<option value="as">অসমীয়া</option>
Problem is that when i select od / as from menu, url changes to:
<domain>/en/od/myhelp/
<domain>/en/as/myhelp/
basically, en is not removed from url locale causing page not found error. Any help or indication in right direction to correctly add this locale is appreciated.
version:
django-cms: 3.7.1
Django: 2.1
basic code:
urls.py
admin.autodiscover()
urlpatterns = [
url(r'^events/(?P<path>.*)$', EventsProxyView.as_view()),
url(r'^sitemap\.xml$', sitemap,
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^healthcheck/$', healthcheck)
]
urlpatterns += i18n_patterns(
url(r'^admin/login', home),
url(r'^admin/', admin.site.urls), # NOQA
url(r'^', include('cms.urls')),
)
urlpatterns += [
url('', include('social_django.urls', namespace='social')),
]
relevant part from settings.xml
import os # isort:skip
from urllib.parse import urljoin
gettext = lambda s: s
DATA_DIR = os.path.dirname(os.path.dirname(__file__))
import os
from django.conf import global_settings
import django.conf.locale
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = [
'*',
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'cms', 'templates'),],
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.template.context_processors.csrf',
'django.template.context_processors.tz',
'sekizai.context_processors.sekizai',
'django.template.context_processors.static',
'cms.context_processors.cms_settings',
],
'loaders': [
('django.template.loaders.cached.Loader', [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
])],
},
},
]
MIDDLEWARE = [
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'redirection.middleware.MyHelpRedirectMiddleware',
'djangocms_redirect.middleware.RedirectMiddleware',
'django.middleware.security.SecurityMiddleware',
'cms.middleware.HostsPrinterMiddleware',
'cms.middleware.DisableCSRF',
'cms.middleware.utils.ApphookReloadMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
]
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'cms', 'scripts', 'dist', 'bundles', 'webpack-stats.json'),
}
}
INSTALLED_APPS = [
'djangocms_admin_style',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.messages',
'cms',
'menus',
'sekizai',
'treebeard',
'djangocms_text_ckeditor',
'filer',
'easy_thumbnails',
'djangocms_column',
'djangocms_file',
'djangocms_link',
'djangocms_picture',
'djangocms_style',
'djangocms_snippet',
'djangocms_googlemap',
'djangocms_video',
'social_django',
'revproxy',
'djangocms_redirect',
'redirection',
'webpack_loader',
'django_select2',
'accordion'
]
LANGUAGES = (
## Customize this
('en', gettext('en')),
('hi', gettext('hi')),
('mr', gettext('mr')),
('te', gettext('te')),
('ta', gettext('ta')),
('kn', gettext('kn')),
('gu', gettext('gu')),
('bn', gettext('bn')),
('ml', gettext('ml')),
('as', gettext('as')),
('od', gettext('od'))
)
CMS_LANGUAGES = {
## Customize this
1: [
{
'code': 'en',
'name': gettext('en'),
'redirect_on_fallback': True,
'public': True,
'hide_untranslated': False,
},
{
'code': 'hi',
'name': gettext('hi'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'mr',
'name': gettext('mr'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'te',
'name': gettext('te'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'ta',
'name': gettext('ta'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'kn',
'name': gettext('kn'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'gu',
'name': gettext('gu'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'bn',
'name': gettext('bn'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'ml',
'name': gettext('ml'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'od',
'name': gettext('od'),
'public': True,
'fallbacks': ['en'],
},
{
'code': 'as',
'name': gettext('as'),
'public': True,
'fallbacks': ['en'],
}
],
'default': {
'fallbacks': ['en', 'hi'],
'redirect_on_fallback': True,
'public': True,
'hide_untranslated': False,
},
}
CMS_TEMPLATES = (
## Customize this
('fullwidth.html', 'Fullwidth'),
('faq_fullwidth.html', 'FAQ Fullwidth'),
('faq_article.html', 'FAQ Article'),
('faq_article_with_feedback.html', 'FAQ Article with Feedback'),
)
CMS_PERMISSION = True
CMS_PLACEHOLDER_CONF = {}
MIGRATION_MODULES = {
}
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
'easy_thumbnails.processors.filters'
)
CMS_MYHELP_REDIRECT_USE_REQUEST = True
CMS_MYHELP_REDIRECT_ALLOWED_PATHS = {
'/myhelp',
'/myhelp/',
'/en/myhelp',
'/en/myhelp/',
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
