2

I'm planning to add change language dropdown in my admin page. according to this code and How to extend admin page.
I copy base_site.html and copy it to myapp/templates/admin, the i create a html file named change_language.html and write this code in it:

{% load i18n %}
/ {% trans 'Change language' %}
<form action="/i18n/setlang/" method="post" style="display: inline;">
  <div style="display: inline;">
    <select name="language" onchange="javascript:form.submit()">
      {% for lang in LANGUAGES %}
        <option value="{{ lang.0 }}"{% ifequal LANGUAGE_CODE lang.0 %} selected="selected"{% endifequal %}>{{ lang.1 }}</option>
      {% endfor %}
    </select>
  </div>
</form>

I add {% extends 'admin/base_site.html' %} at the top of this file, noting happens.
I add {% extends 'admin/base.html' %} , again noting happens.
All hints and answers says that we should change something name <div id="user-tools"> at line 25 of base.html, But in Django 1.10 it goes to line 31 with a different staff. im kinnda lost because i read many different staff every where and non of them works for me. Dose any know where im doing wrong ?
here is my middlewares :

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

And template settings :

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        os.path.join(BASE_DIR,'templates'),
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]
Community
  • 1
  • 1
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59

3 Answers3

2

In your templates/admin folder, make sure the file is named base_site.html (otherwise the default base_site will not be overwritten).

Make sure to copy the latest version of the file from the django-repo.

Most important: verify you extend the admins base.html (your base_site.html should start with {% extends "admin/base.html" %})

You may now add your form (for example to the footer):

{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

{% block footer %}
<div id="footer">
    <form action="/i18n/setlang/" method="post">
    {% csrf_token %}
        <select name="language">
            {% for lang in LANGUAGES %}
                <option value="{{ lang.0 }}"
                    {% ifequal lang.0 request.LANGUAGE_CODE %}
                        selected="yes"
                    {% endifequal %}
                >{{ lang.1 }}</option>
            {% endfor %}
        </select>
        <input type="submit" value="{% trans 'Change language' %}" />
    </form>
</div>
{% endblock %}

Refer to the base.html to find a block that seems suitable for your form.

You will also need to add the i18n urls to your url-settings

url(r'^i18n/', include('django.conf.urls.i18n')),

And if you really want to submit your form using javascript you will also need to get and submit the csrf token for the form. The django docs cover this topic quite comprehensive.

Kim
  • 1,361
  • 3
  • 18
  • 24
2

In addition to Kim's answer, you should add i18n context processor in your django settings django.template.context_processors.i18n to be able to access LANGUAGES variable in the template.

1

I guess you are mixing both answers that you've found on the internet. One of them copies and changes a couple of files from the admin template, effectively overriding them in your program's references. The second one extends admin's templates. You should completely implement just one of them.

navid
  • 566
  • 6
  • 15
  • Can you be more specific ? if i want to copy the base.html file, where should i copy it ? and how it works ? – Mehrdad Pedramfar Aug 28 '16 at 14:18
  • 1
    here is the code from base.html https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base.html – navid Aug 29 '16 at 07:02
  • 1
    your first link suggests copying this and changing line 25 (or 28 in this example) tp include your new html file (change_language.html) – navid Aug 29 '16 at 07:02
  • 1
    your second answer suggests creating another html file (for example base_site.html so it doesn't conflict with base.html) and extending a block in the original base.html. In this case you should replace the userlinks block with the new code. – navid Aug 29 '16 at 07:04
  • Do you know how can i access the LANGUAGES ? in my `change_languages.html` the `select` comes with no option ? can you help me ? @navit – Mehrdad Pedramfar Aug 29 '16 at 12:14
  • not helping... my languages are sets. and works correctly with urls.. it just not load in the select options. – Mehrdad Pedramfar Aug 29 '16 at 12:41
  • It is just not different from any other template. Anyhow you are loading your language in a normal template, you can do this here. – navid Aug 30 '16 at 12:48