Login

showing environment variables in the django admin

Author:
tonemcd
Posted:
January 24, 2010
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

Having things like DATABASE_NAME in the admin interface is handy if you're working on development and deployment systems.

Replace the template admin/base_site.html with the template code.

The variables to be displayed in the admin need to be exported into the environment before running the server.

The python code shown is an example of a wsgi handler, and the same format can be used in manage.py for the development server.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Replace admin/base_site.html with this;

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

{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}

{% block branding %}
<h1 id="site-name">Django admin on {{request.META.SERVER_NAME}}, using {{request.META.DJANGO_SETTINGS_MODULE}}</h1>
<h2 id="site-url"><a href="/admin">root</a>&nbsp;<a href="/admin/doc/">Documentation</a></h2>
{% endblock %}
{% block nav-global %}Database being used:<span style="color:red;">{{request.META.DATABASE_NAME}}</span>&nbsp;{% endblock %}


Example for a WSGI server

import os, sys
import settings
sys.path.append('/www/project_location')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
os.environ['DATABASE_NAME'] = settings.DATABASE_NAME

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Example for manage.py

#!/usr/bin/env python
import os
from django.core.management import execute_manager
try:
    import settings # Assumed to be in the same directory.
    os.environ['DATABASE_NAME'] = settings.DATABASE_NAME
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It ap
pears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module
.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(settings)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.