15.06.2017       Выпуск 182 (12.06.2017 - 18.06.2017)       Статьи

5 способов сделать Django Admin безопаснее Django

Читать>>



Экспериментальная функция:

Ниже вы видите текст статьи по ссылке. По нему можно быстро понять ссылка достойна прочтения или нет

Просим обратить внимание, что текст по ссылке и здесь может не совпадать.

Visually distinguish environments

Users and admins are not perfect and mistakes happen. When you have multiple environments such as development, QA, staging and production, it’s not unlikely for an admin to perform a destructive operation in the wrong environment by accident (just ask gitlab).

To reduce the chance of mistakes, we mark different environments clearly in the admin:

Indicator in different environments

First you need to have some way of knowing which environment you are on. We have a variable called ENVIRONMENT_NAME we populate during deployment. We have another variable called ENVIRONMENT_COLOR for the indicator color.

To add the environment indicator to every page in the admin, override the base admin template:

# app/templates/admin/base_site.html
{% extends “admin/base_site.html” %}
{% block extrastyle %}
<style type=”text/css”>
body:before {
display: block;
line-height: 35px;
text-align: center;
font-weight: bold;
text-transform: uppercase;
color: white;
content: “{{ ENVIRONMENT_NAME }}”;
background-color: {{ ENVIRONMENT_COLOR }};

}
</style>









{% endblock %}

To make the ENVIRONMENT variables from settings.py available in the template we use a context processor:

# app/context_processors.py
from django.conf import settings
def from_settings(request):
return {
'ENVIRONMENT_NAME': settings.ENVIRONMENT_NAME,
'ENVIRONMENT_COLOR': settings.ENVIRONMENT_COLOR,
}



To register the context processor add the following in settings.py:

TEMPLATES = [{

'OPTIONS': {
'context_processors': [

'app.context_processors.from_settings',
],

},
}]








Now when you open Django Admin you should see the indicator on top.



Лучшая Python рассылка

Нас поддерживает


Python Software Foundation



Разместим вашу рекламу

Пиши: mail@pythondigest.ru

Нашли опечатку?

Выделите фрагмент и отправьте нажатием Ctrl+Enter.

Система Orphus