Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3311378
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:52:11+00:00 2026-05-17T21:52:11+00:00

I want to have some constants in a Django Projects. For example, let’s say

  • 0

I want to have some constants in a Django Projects. For example, let’s say a constant called MIN_TIME_TEST.

I would like to be able to access this constant in two places: from within my Python code, and from within any Templates.

What’s the best way to go about doing this?

EDIT:
To clarify, I know about Template Context Processors and about just putting things in settings.py or some other file and just importing.

My question is, how do I combine the two approaches without violating the “Don’t Repeat Yourself” rule? Based on the answers so far, here’s my approach:

I’d like to create a file called global_constants.py, which will have a list of constants (things like MIN_TIME_TEST = 5). I can import this file into any module to get the constants.

But now, I want to create the context processor which returns all of these constants. How can I go about doing this automatically, without having to list them again in a dictionary, like in John Mee’s answer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:52:12+00:00Added an answer on May 17, 2026 at 9:52 pm

    Both Luper and Vladimir are correct imho but you’ll need both in order to complete your requirements.

    • Although, the constants don’t need to be in the settings.py, you could put them anywhere and import them from that place into your view/model/module code. I sometimes put them into the __init__.py if I don’t care to have them to be considered globally relevant.

    • a context processor like this will ensure that selected variables are globally in the template scope

      def settings(request):
          """
          Put selected settings variables into the default template context
          """
          from django.conf import settings
          return {
              'DOMAIN':     settings.DOMAIN,
              'GOOGLEMAPS_API_KEY': settings.GOOGLEMAPS_API_KEY,
          }
      

    But this might be overkill if you’re new to django; perhaps you’re just asking how to put variables into the template scope…?

    from django.conf import settings
    
    ...
    # do stuff with settings.MIN_TIME_TEST as you wish
    
    render_to_response("the_template.html", { 
        "MIN_TIME_TEST": settings.MIN_TIME_TEST 
    }, context_instance=RequestContext(request)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have settings.php file that I want to keep some constants in, like my
I have some code which I want to document with in-body comments like so:
As per subject. I have some constants hash defined like so: #define CONST 40
I have a class in which I want to define some constants used by
Let's say I have a class defined something like the below: namespace MyProject.MyConstants {
I have some text which I want to have on separate lines. I try
I want to have some kind of single list that is initialized in a
I have a class and I want to have some bit masks with values
if i want to have some text show up in a calligraphy font, how
I am writing some BASH script and I want it have some error handling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.