Fwd: Global Variable

50 views
Skip to first unread message

Sudipta Sen

unread,
Nov 4, 2014, 8:23:46 AM11/4/14
to django...@googlegroups.com


---------- Forwarded message ---------
From: Sudipta Sen <sanbo...@gmail.com>
Date: Tue Nov 04 2014 at 6:46:17 PM
Subject: Global Variable
To: <django...@googlegroups.com>


Hi folks, 

I am very new to django and facing some problems. 

1. I need a global variable, which I can use throughout the application. Like let's say base_url. 
  • I tried defining one variable in settings and using it places like this 
    • from django.conf import settings
    • url = settings.BASE_URL+'login'
  • This gives me an error : non-keyword arg after keyword arg
  • Also this process seems to be painful, every time I have to import settings in each of my views.
2. I need a custome context variable to be passed everytime. 
  • I tried this: in the same directory where my settings is, I created a context_processors.py
  • context_processors.py:
    • def baseurl(request):
      • return {"base_url" : "/some/url/"}
  • In my settings.py file I am adding this line :
    • TEMPLATE_CONTEXT_PROCESSORS += ('context_processors.baseurl',)
    • This gives me an error : NameError: name 'TEMPLATE_CONTEXT_PROCESSORS' is not defined
Please help me on these issues.

Andreas Kuhne

unread,
Nov 4, 2014, 8:30:41 AM11/4/14
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAO0Um5FCybTFF9BeJn%3DTw755EFsCVhL8TEw14pjQTxnP5muwtA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Hi Sudipta,

You can do this in one of 2 ways:
1. Add it to your settings file, like you have done. I would however write : url = "%s/login" % settings.BASE_URL instead.
2. Add a context processor. You should in your settings.py add:
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request",
    "context_processors.baseurl",
)

That includes the normally added context processors (at least those that we have in our project at the moment).

Regards,

Andréas

Sudipta Sen

unread,
Nov 4, 2014, 8:30:50 AM11/4/14
to django...@googlegroups.com
By the way I am using virtual environment and my django version is 1.7.1

Collin Anderson

unread,
Nov 5, 2014, 12:36:01 PM11/5/14
to django...@googlegroups.com
Hello,

You could also try creating an assignment tag something like:

{% get_base_url as base_url %}


That way you don't need to deal with context_processors.

Collin

Reply all
Reply to author
Forward
0 new messages