Django translate refers to wrong URL
I am using Django (version 2.1.3) in one of my project and currently struggle with a weird bug. I use the build in internationalization module and included a language toggler in my main menu which is loaded on every page
% get_current_language as LANGUAGE_CODE %
<form id="form" action="% url 'set_language' %" method="post">
% csrf_token %
<input name="next" type="hidden" value="strip_lang " />
<input id="form_lang" name="language" type="hidden" value=" LANGUAGE_CODE "/>
</form>
<ul role="menu" class="dropdown-menu"id="lang-dropdown">
% get_available_languages as LANGUAGES %
% get_language_info_list for LANGUAGES as languages %
% for language in languages %
<li>
<a href="#" onClick='(function()
document.getElementById("form_lang").value = " language.code ";
document.getElementById("form").submit(); return false;)();return false;'>
language.name_local ( language.code )</a>
</li>
% endfor %
</ul>
I created a special tag "strip_lang" to strip the language identifier from the current URL.
@register.filter
@stringfilter
def strip_lang(value):
"""Removes all values of arg from the given string"""
lang = getattr(settings, "LANGUAGES", None)
url = value.split('/')
if url[1] in [l[0] for l in lang]:
return urllib.parse.unquote('/' + '/'.join(value.split('/')[2:]))
else:
return urllib.parse.unquote(value)
This all works well. However, for one of my apps, I always get rerouted to a different app.
ie if I am at path
/en/app1/mypage1
and toggle the language i suddenly end up at
/fr/app2/mypage1
*****EDIT*****
Referals for app2 work correctly. When entering a URL manually for app1 the pages load correctly.
My url.py in the main project looks like this
urlpatterns = [
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += i18n_patterns(
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
path('app3/', include('app3.urls')),
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^login/$', auth_views.LoginView.as_view(), 'template_name': 'login.html', name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(), 'template_name': 'logged_out.html', name='logout'),
url(r'^oauth/', include('social_django.urls', namespace='social')),
prefix_default_language=False)
ans urls.py of my apps look like this
urlpatterns = [
path('', views.index, name='index'),
path('surveys/', views.nrgt_surveys, name='nrgt_surveys'),
path('surveys/<str:survey_name>/', views.survey, name='survey'),
path('surveys/<str:survey_name>/<query_name>', views.survey_query, name='survey_query'),
path('landscapes/', views.landscapes, name='landscapes'),
path('landscapes/<str:landscape_name>/', views.landscape, name='landscape'),
path('landscapes/<str:landscape_name>/<query_name>', views.landscape_query, name='landscape_query'),
]
I do not specify the app name anywhere in my code. When I track my network requests I can also see that the correct URL is submitted in the POST request. Why does my url get modified?
django python-3.x django-i18n
add a comment |
I am using Django (version 2.1.3) in one of my project and currently struggle with a weird bug. I use the build in internationalization module and included a language toggler in my main menu which is loaded on every page
% get_current_language as LANGUAGE_CODE %
<form id="form" action="% url 'set_language' %" method="post">
% csrf_token %
<input name="next" type="hidden" value="strip_lang " />
<input id="form_lang" name="language" type="hidden" value=" LANGUAGE_CODE "/>
</form>
<ul role="menu" class="dropdown-menu"id="lang-dropdown">
% get_available_languages as LANGUAGES %
% get_language_info_list for LANGUAGES as languages %
% for language in languages %
<li>
<a href="#" onClick='(function()
document.getElementById("form_lang").value = " language.code ";
document.getElementById("form").submit(); return false;)();return false;'>
language.name_local ( language.code )</a>
</li>
% endfor %
</ul>
I created a special tag "strip_lang" to strip the language identifier from the current URL.
@register.filter
@stringfilter
def strip_lang(value):
"""Removes all values of arg from the given string"""
lang = getattr(settings, "LANGUAGES", None)
url = value.split('/')
if url[1] in [l[0] for l in lang]:
return urllib.parse.unquote('/' + '/'.join(value.split('/')[2:]))
else:
return urllib.parse.unquote(value)
This all works well. However, for one of my apps, I always get rerouted to a different app.
ie if I am at path
/en/app1/mypage1
and toggle the language i suddenly end up at
/fr/app2/mypage1
*****EDIT*****
Referals for app2 work correctly. When entering a URL manually for app1 the pages load correctly.
My url.py in the main project looks like this
urlpatterns = [
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += i18n_patterns(
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
path('app3/', include('app3.urls')),
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^login/$', auth_views.LoginView.as_view(), 'template_name': 'login.html', name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(), 'template_name': 'logged_out.html', name='logout'),
url(r'^oauth/', include('social_django.urls', namespace='social')),
prefix_default_language=False)
ans urls.py of my apps look like this
urlpatterns = [
path('', views.index, name='index'),
path('surveys/', views.nrgt_surveys, name='nrgt_surveys'),
path('surveys/<str:survey_name>/', views.survey, name='survey'),
path('surveys/<str:survey_name>/<query_name>', views.survey_query, name='survey_query'),
path('landscapes/', views.landscapes, name='landscapes'),
path('landscapes/<str:landscape_name>/', views.landscape, name='landscape'),
path('landscapes/<str:landscape_name>/<query_name>', views.landscape_query, name='landscape_query'),
]
I do not specify the app name anywhere in my code. When I track my network requests I can also see that the correct URL is submitted in the POST request. Why does my url get modified?
django python-3.x django-i18n
post your urls.py ?
– a_k_v
Nov 15 '18 at 4:53
Thanks @a_k_v, I added the relevant part from my urls.py
– Thomas
Nov 15 '18 at 13:53
have you tried to remove the line<input name="next" type="hidden" value="strip_lang " />
entirely? I never use this hidden value in my projects and the i18n works without a problem.
– nik_m
Feb 16 at 20:12
add a comment |
I am using Django (version 2.1.3) in one of my project and currently struggle with a weird bug. I use the build in internationalization module and included a language toggler in my main menu which is loaded on every page
% get_current_language as LANGUAGE_CODE %
<form id="form" action="% url 'set_language' %" method="post">
% csrf_token %
<input name="next" type="hidden" value="strip_lang " />
<input id="form_lang" name="language" type="hidden" value=" LANGUAGE_CODE "/>
</form>
<ul role="menu" class="dropdown-menu"id="lang-dropdown">
% get_available_languages as LANGUAGES %
% get_language_info_list for LANGUAGES as languages %
% for language in languages %
<li>
<a href="#" onClick='(function()
document.getElementById("form_lang").value = " language.code ";
document.getElementById("form").submit(); return false;)();return false;'>
language.name_local ( language.code )</a>
</li>
% endfor %
</ul>
I created a special tag "strip_lang" to strip the language identifier from the current URL.
@register.filter
@stringfilter
def strip_lang(value):
"""Removes all values of arg from the given string"""
lang = getattr(settings, "LANGUAGES", None)
url = value.split('/')
if url[1] in [l[0] for l in lang]:
return urllib.parse.unquote('/' + '/'.join(value.split('/')[2:]))
else:
return urllib.parse.unquote(value)
This all works well. However, for one of my apps, I always get rerouted to a different app.
ie if I am at path
/en/app1/mypage1
and toggle the language i suddenly end up at
/fr/app2/mypage1
*****EDIT*****
Referals for app2 work correctly. When entering a URL manually for app1 the pages load correctly.
My url.py in the main project looks like this
urlpatterns = [
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += i18n_patterns(
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
path('app3/', include('app3.urls')),
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^login/$', auth_views.LoginView.as_view(), 'template_name': 'login.html', name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(), 'template_name': 'logged_out.html', name='logout'),
url(r'^oauth/', include('social_django.urls', namespace='social')),
prefix_default_language=False)
ans urls.py of my apps look like this
urlpatterns = [
path('', views.index, name='index'),
path('surveys/', views.nrgt_surveys, name='nrgt_surveys'),
path('surveys/<str:survey_name>/', views.survey, name='survey'),
path('surveys/<str:survey_name>/<query_name>', views.survey_query, name='survey_query'),
path('landscapes/', views.landscapes, name='landscapes'),
path('landscapes/<str:landscape_name>/', views.landscape, name='landscape'),
path('landscapes/<str:landscape_name>/<query_name>', views.landscape_query, name='landscape_query'),
]
I do not specify the app name anywhere in my code. When I track my network requests I can also see that the correct URL is submitted in the POST request. Why does my url get modified?
django python-3.x django-i18n
I am using Django (version 2.1.3) in one of my project and currently struggle with a weird bug. I use the build in internationalization module and included a language toggler in my main menu which is loaded on every page
% get_current_language as LANGUAGE_CODE %
<form id="form" action="% url 'set_language' %" method="post">
% csrf_token %
<input name="next" type="hidden" value="strip_lang " />
<input id="form_lang" name="language" type="hidden" value=" LANGUAGE_CODE "/>
</form>
<ul role="menu" class="dropdown-menu"id="lang-dropdown">
% get_available_languages as LANGUAGES %
% get_language_info_list for LANGUAGES as languages %
% for language in languages %
<li>
<a href="#" onClick='(function()
document.getElementById("form_lang").value = " language.code ";
document.getElementById("form").submit(); return false;)();return false;'>
language.name_local ( language.code )</a>
</li>
% endfor %
</ul>
I created a special tag "strip_lang" to strip the language identifier from the current URL.
@register.filter
@stringfilter
def strip_lang(value):
"""Removes all values of arg from the given string"""
lang = getattr(settings, "LANGUAGES", None)
url = value.split('/')
if url[1] in [l[0] for l in lang]:
return urllib.parse.unquote('/' + '/'.join(value.split('/')[2:]))
else:
return urllib.parse.unquote(value)
This all works well. However, for one of my apps, I always get rerouted to a different app.
ie if I am at path
/en/app1/mypage1
and toggle the language i suddenly end up at
/fr/app2/mypage1
*****EDIT*****
Referals for app2 work correctly. When entering a URL manually for app1 the pages load correctly.
My url.py in the main project looks like this
urlpatterns = [
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += i18n_patterns(
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
path('app3/', include('app3.urls')),
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^login/$', auth_views.LoginView.as_view(), 'template_name': 'login.html', name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(), 'template_name': 'logged_out.html', name='logout'),
url(r'^oauth/', include('social_django.urls', namespace='social')),
prefix_default_language=False)
ans urls.py of my apps look like this
urlpatterns = [
path('', views.index, name='index'),
path('surveys/', views.nrgt_surveys, name='nrgt_surveys'),
path('surveys/<str:survey_name>/', views.survey, name='survey'),
path('surveys/<str:survey_name>/<query_name>', views.survey_query, name='survey_query'),
path('landscapes/', views.landscapes, name='landscapes'),
path('landscapes/<str:landscape_name>/', views.landscape, name='landscape'),
path('landscapes/<str:landscape_name>/<query_name>', views.landscape_query, name='landscape_query'),
]
I do not specify the app name anywhere in my code. When I track my network requests I can also see that the correct URL is submitted in the POST request. Why does my url get modified?
django python-3.x django-i18n
django python-3.x django-i18n
edited Nov 15 '18 at 14:07
Thomas
asked Nov 15 '18 at 2:22
ThomasThomas
605310
605310
post your urls.py ?
– a_k_v
Nov 15 '18 at 4:53
Thanks @a_k_v, I added the relevant part from my urls.py
– Thomas
Nov 15 '18 at 13:53
have you tried to remove the line<input name="next" type="hidden" value="strip_lang " />
entirely? I never use this hidden value in my projects and the i18n works without a problem.
– nik_m
Feb 16 at 20:12
add a comment |
post your urls.py ?
– a_k_v
Nov 15 '18 at 4:53
Thanks @a_k_v, I added the relevant part from my urls.py
– Thomas
Nov 15 '18 at 13:53
have you tried to remove the line<input name="next" type="hidden" value="strip_lang " />
entirely? I never use this hidden value in my projects and the i18n works without a problem.
– nik_m
Feb 16 at 20:12
post your urls.py ?
– a_k_v
Nov 15 '18 at 4:53
post your urls.py ?
– a_k_v
Nov 15 '18 at 4:53
Thanks @a_k_v, I added the relevant part from my urls.py
– Thomas
Nov 15 '18 at 13:53
Thanks @a_k_v, I added the relevant part from my urls.py
– Thomas
Nov 15 '18 at 13:53
have you tried to remove the line
<input name="next" type="hidden" value="strip_lang " />
entirely? I never use this hidden value in my projects and the i18n works without a problem.– nik_m
Feb 16 at 20:12
have you tried to remove the line
<input name="next" type="hidden" value="strip_lang " />
entirely? I never use this hidden value in my projects and the i18n works without a problem.– nik_m
Feb 16 at 20:12
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311522%2fdjango-translate-refers-to-wrong-url%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311522%2fdjango-translate-refers-to-wrong-url%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
post your urls.py ?
– a_k_v
Nov 15 '18 at 4:53
Thanks @a_k_v, I added the relevant part from my urls.py
– Thomas
Nov 15 '18 at 13:53
have you tried to remove the line
<input name="next" type="hidden" value="strip_lang " />
entirely? I never use this hidden value in my projects and the i18n works without a problem.– nik_m
Feb 16 at 20:12