1 year ago

#384078

test-img

viviwill

Django i18n does not work with Nginx. Keep redirecting to homepage

Right now I have a django + gunicorn + nginx setup.

Everything works:

  1. able to switch between the two language
  2. set the cookies
  3. add the lang prefix in the middle: projurl.com/lang_code/other_dir
  4. able to redirect/stay on the page where language switch happened

But ...

  • All 1,2,3,4 are working with django dev server (python manage.py runserver) and gunicorn proj.wsgi:application --bind 0.0.0.0:8000

  • Only 1,2,3 work with nginx. It keeps redirecting to homepage with the right language, but unable to stay on the page where language switch happened.

proj/urls.py:

urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += i18n_patterns(
    re_path(r'^admin/', admin.site.urls),
    re_path(r'', include('app1.urls')),
)

In template I have exact example from django i18n translation: link

{% 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> 

nginx.conf

upstream proj {
    server web:8000;
}

server {
    listen 80;

    location /static/ {
        alias /static/;
    }

    location / {
        proxy_pass http://proj;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass_header "Accept-Language"; # <<== tried this, does not work
        proxy_redirect off;
    }

python

django

nginx

internationalization

0 Answers

Your Answer

Accepted video resources