1

Is there a way to access the ADMINS variable of the settings module from an any arbitrary template without adding manually adding it into the context before being rendered, similar to how request is available in any template using RequestContext if django.core.context_processors.request is in TEMPLATE_CONTEXT_PROCESSORS?

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
user193130
  • 8,009
  • 4
  • 36
  • 64

2 Answers2

6

You can write your own context processor (which is a regular function that has request as parameter):

from django.conf import settings

def admin_emails(request):
    return { 'ADMINS': settings.ADMINS }

and add path.to.my.context_processor.admin_emails to TEMPLATE_CONTEXT_PROCESSORS.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
2

I'd use a template tag like discussed for this question: Can I access constants in settings.py from templates in Django?

Specifically I use the code from this answer: https://stackoverflow.com/a/6343321/2250326

With that you can get at the AMDINS in your templates like this:

{% value_from_settings "ADMINS" as admins %}
{% for admin in admins %}
    Name: {{ admin.0 }}<br />
    Email: {{ admin.1 }}
{% endfor %}
Community
  • 1
  • 1
caipirginka
  • 266
  • 3
  • 4