PhantomS
PhantomS

Reputation: 490

set_lanuguage is not changing the language of the enitre webpage and miss the redirections by views

My form with language selection buttons and an update button. Customer update form is implemented through django forms and views however language selections are directly implemented in html file using below code:

{% load i18n %}

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}" />
    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}

            <option value="{{ language.code }}"{% if language.code ==     LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
    <input type="submit" value="Go" />
</form>

1:image of webpage rendered form

Whenever I select any language same page with default english is rendered again.

I have placed the relevant context processors and locale middleware where it should be in settings file.

My root urlConf is given below:

urlpatterns = [
    url(r'^', include('custupdate.urls')),
    url(r'^i18n/', include('django.conf.urls.i18n')),
]

urlpatterns += i18n_patterns(
    url(r'^', include('custupdate.urls')),

My application url conf is given:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^personBasic', views.person_detail, name='person_detail'),
    url(r'^licenseeBasic', views.licensee_detail, name='licensee_detail'),
    url(r'^address', views.address, name='address'),
    url(r'^contact', views.contact, name='contact'),
    url(r'^email', views.email, name='email'),
]

My each url e.g personBasic is hooked to its relevant view and view is hooked with relevant form.

Now when I enter following url in address bar and hit enter http://mycompany.com/custupdate Because of above url configurations it goes to index view which redirects to following url http://mycompany.com/custupdate/personBasic and image 1 is shown in browser. So selecting any language renders the same page again without change of language.

When I do inspect element in the browser for any language button when its pressed I get following url requested http://mycompany.com/custupdate/i18n/setlang/

instead of http://mycompany.com/custupdate/personBasic/i18n/setlang/

Question is when the language buttons are pressed shouldn't the whole url in address bar be rendered as translated version. Whatever is after custupdate is skipped somehow and i18n/setlang is appended after custupdate where i'm expecting it to be like this /custupdate/personBasic/i18n/setlang/ or /custupdate/contact/i18n/setlang/ or /custupdate/email/i18n/setlang/

Upvotes: 1

Views: 52

Answers (1)

nik_m
nik_m

Reputation: 12096

Change your root URLconf to this:

urlpatterns = [
    url(r'^i18n/', include('django.conf.urls.i18n')),
]

urlpatterns += i18n_patterns(
    url(r'^', include('custupdate.urls')),
)

Also, inside your form, remove the hidden input named next, completely. You have not defined a redirect_to variable.

Also, make sure that you have set middlewares in the correct order.

Upvotes: 1

Related Questions