changing language redirect

58 views
Skip to first unread message

Yahiba

unread,
Jan 21, 2020, 1:59:20 PM1/21/20
to django-oscar
Hello,

I have the option to change the language in nav_accounts.html like so:

{% block nav_account_languages %}
            {% if LANGUAGES|length > 1 %}
                <form id="language_selector" class="navbar-left navbar-form" action="{% url 'set_language' %}" method="post">
                    {% csrf_token %}
                    <input name="next" type="hidden" value="{{ language_neutral_url_path }}" />
                    <div class="form-group">
                        <select name="language" class="form-control">
                            {% get_language_info_list for LANGUAGES as languages %}
                            {% for language in languages %}
                                <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE%} selected="selected"{% endif %}>
                                    {{ language.name_local }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <button class="btn btn-default" type="submit" data-loading-text="{% trans 'Submitting...' %}">{% trans "Go" %}</button>
                </form>
            {% endif %}
            {% endblock %}

However, when I change the language, I am redirected to




I want to stay on the same page when the language is changed.

so if I am in 


and change the language

I continue in 



Anyone knows how can I do this?





thanks


Samir Shah

unread,
Jan 22, 2020, 12:01:49 AM1/22/20
to django-oscar
`{{ language_neutral_url_path }}` should be doing this. If that hidden input is empty, it probably means that you're missing `oscar.core.context_processors.metadata` from your template context processors setting.

Yahiba

unread,
Jan 22, 2020, 6:35:06 AM1/22/20
to django-oscar
hello Samir,

I checked it out, and I have it:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        'OPTIONS': {
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.request',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',

                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.contrib.messages.context_processors.messages',

                # Oscar specific
                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.core.context_processors.metadata',
            ],
            'debug': DEBUG,
        }
    }
]


In fact {{ language_neutral_url_path }} is empty, I printed this and I only get a slash ( / )
what could this be?

Samir Shah

unread,
Jan 22, 2020, 6:53:04 AM1/22/20
to django-oscar
I'm not too sure what the issue is then. Do you have `USE_I18N` set to True in your settings? That is the only other thing that would short-circuit the context variable.

Yahiba

unread,
Jan 22, 2020, 7:36:53 AM1/22/20
to django-oscar
Hello again Samir,

I now noticed the the {{ language_neutral_url_path }} will be equal to the directory I am on, so previously it was just a slash ( / ) because I was in the root directory, so if I switch to the catalogue it will be ( /catalogue/ ) 



I have the following configurations:


USE_I18N = True

USE_L10N = True

USE_TZ = True


I tried turnig False USE_I18N but then the language switching does not work.

Samir Shah

unread,
Jan 22, 2020, 7:56:58 AM1/22/20
to django...@googlegroups.com
That seems to be what you would want? So the next URL is set to be the same as the current URL without the language code. You should then get redirected to the new language code with this URL path appended. 

--
https://github.com/django-oscar/django-oscar
http://django-oscar.readthedocs.org/en/latest/
https://twitter.com/django_oscar
---
You received this message because you are subscribed to a topic in the Google Groups "django-oscar" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-oscar/QPsfCnbbuzU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-oscar...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/django-oscar/27a96c67-3ba5-4764-bc31-9e5b8330f2b6%40googlegroups.com.

Yahiba

unread,
Jan 22, 2020, 9:37:24 AM1/22/20
to django-oscar
Hello again Samir,

I noticed this is related to my previous post. If I am in a product directory and change the language, I am redirected to that same product in the new language selected.

Now if am at the root and change the language, I am redirected to /catalogue ( in the new language).

In this specific case I want to remain at the root directory(with the new language). All the others cases seem to be working correctly.

here is my urls.py main file:

from Store import views
urlpatterns = [
    url(r'^$', views.home, name ='home'),
    url(r'^sobre-nos/$', views.about, name ='about'),
    url(r'^dashboard/translation$',views.dashboard_translation, name="dashboard_translation"),
    url(r'^dashboard/sobre-nos/$', views.sobre_nos, name='dashboard-sobre-nos'),
    url(r'^dashboard/bem-estar/$', views.bem_estar, name='dashboard-bem-estar'),
    url(r'^dashboard/home-page/$', views.home_page, name='dashboard-home-page'),

]

from django.contrib.sitemaps import views

urlpatterns += [



    path('store/', include('Store.urls')),

    path('bem-estar/', include('myblog.urls')),
    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('i18n/', include('django.conf.urls.i18n')),  # > Django-2.0
    path('admin/', admin.site.urls),  # > Django-2.0

    path('', include(apps.get_app_config('oscar').urls[0])),  # > Django-2.0


    url(r'^sitemap\.xml$', views.index,
        {'sitemaps': base_sitemaps}),
    url(r'^sitemap-(?P<section>.+)\.xml$', views.sitemap,
        {'sitemaps': base_sitemaps},
        name='django.contrib.sitemaps.views.sitemap'),



]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



I had to do this because oscar root is redirected to /catalogue.
But now when I am at the root and switch language, I continue to be redirected to /catalogue




quarta-feira, 22 de Janeiro de 2020 às 12:56:58 UTC, Samir Shah escreveu:
That seems to be what you would want? So the next URL is set to be the same as the current URL without the language code. You should then get redirected to the new language code with this URL path appended. 

On Wed, 22 Jan 2020, 15:36 Yahiba, <yahi...@gmail.com> wrote:
Hello again Samir,

I now noticed the the {{ language_neutral_url_path }} will be equal to the directory I am on, so previously it was just a slash ( / ) because I was in the root directory, so if I switch to the catalogue it will be ( /catalogue/ ) 



I have the following configurations:


USE_I18N = True

USE_L10N = True

USE_TZ = True


I tried turnig False USE_I18N but then the language switching does not work.



quarta-feira, 22 de Janeiro de 2020 às 11:53:04 UTC, Samir Shah escreveu:
I'm not too sure what the issue is then. Do you have `USE_I18N` set to True in your settings? That is the only other thing that would short-circuit the context variable.

--
https://github.com/django-oscar/django-oscar
http://django-oscar.readthedocs.org/en/latest/
https://twitter.com/django_oscar
---
You received this message because you are subscribed to a topic in the Google Groups "django-oscar" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-oscar/QPsfCnbbuzU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django...@googlegroups.com.

Yahiba

unread,
Jan 22, 2020, 10:01:06 AM1/22/20
to django-oscar
I'm not sure if this can help, but here it goes the cmd response when I set the language to Spanish

[22/Jan/2020 14:59:07] "POST /i18n/setlang/ HTTP/1.1" 302 0
[22/Jan/2020 14:59:07] "GET /es/ HTTP/1.1" 302 0
[22/Jan/2020 14:59:07] "GET /catalogue/ HTTP/1.1" 200 70334

Samir Shah

unread,
Jan 22, 2020, 10:08:47 AM1/22/20
to django-oscar
Okay, I see where your issue is now. The problem is here:

urlpatterns = [
   url(r'^$', views.home, name ='home'),
]

Your home URL is not internationalised - it only works for / and not for any language-prefixed URL. For any language-prefixed URL you'll end up with Oscar's default home view (which in turn redirects to the catalogue view). You need to wrap it in django.conf.urls.i18n.i18n_patterns (see example in sandbox), something like this:


urlpatterns = i18n_patterns(
   url(r'^$', views.home, name ='home'),
)

Yahiba

unread,
Jan 22, 2020, 10:12:10 AM1/22/20
to django-oscar
what happens is if I go to www.example.com/es/ I am redirected to www.example.com/catalogue

Yahiba

unread,
Jan 22, 2020, 11:25:12 AM1/22/20
to django-oscar
I changed this:


from Store import views
urlpatterns = i18n_patterns(
   url(r'^$', views.home, name ='home'),
)

urlpatterns += [

    # url(r'/es/^$', views.home, name ='home'),
    url(r'^sobre-nos/$', views.about, name ='about'),
    url(r'^dashboard/translation$',views.dashboard_translation, name="dashboard_translation"),
    url(r'^dashboard/sobre-nos/$', views.sobre_nos, name='dashboard-sobre-nos'),
    url(r'^dashboard/bem-estar/$', views.bem_estar, name='dashboard-bem-estar'),
    url(r'^dashboard/home-page/$', views.home_page, name='dashboard-home-page'),

]



from django.contrib.sitemaps import views

urlpatterns += [



    path('store/', include('Store.urls')),

    path('bem-estar/', include('myblog.urls')),
    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('i18n/', include('django.conf.urls.i18n')),  # > Django-2.0
    path('admin/', admin.site.urls),  # > Django-2.0

    path('', include(apps.get_app_config('oscar').urls[0])),  # > Django-2.0


    url(r'^sitemap\.xml$', views.index,
        {'sitemaps': base_sitemaps}),
    url(r'^sitemap-(?P<section>.+)\.xml$', views.sitemap,
        {'sitemaps': base_sitemaps},
        name='django.contrib.sitemaps.views.sitemap'),



]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)




but now I can't access root directory again, I am redirected to catalogue despite the language selection

Yahiba

unread,
Jan 22, 2020, 11:32:31 AM1/22/20
to django-oscar
the closer I got to root is:
or

for instance.


Reply all
Reply to author
Forward
0 new messages