global settings

2 views
Skip to first unread message

bfor...@socialistsushi.com

unread,
Aug 12, 2005, 8:49:53 PM8/12/05
to Django users
Is there a place to set global values in a django project?
Specifically I want to set a base url, so I could do something like

<a href="{{BASE_URL}}/whatever">

I'd like to be able to set this somewhere so it can be easily changed
across installations.

Thanks
--B

Adam

unread,
Aug 16, 2005, 5:44:12 PM8/16/05
to Django users
I posed just this question on #django last week. After going around and
around a few times, here's what I came up with. Its not exactly the
same thing, but it is working for me in a system where all the urls are
basically getting you to detail views of the different objects.

In myproject/settings/main.py add:
APP_URL_ROOT = 'myapp'

This is the url root for the site, (http://localhost:8000/myapp/). This
information lives in only this spot so if it changes to yourapp, I can
just change it in that one spot.

In myproject/settings/urls/main.py:
from django.conf.settings import APP_URL_ROOT
urlpatterns = patterns('',
(r'^%s/' % APP_URL_ROOT,
include('myproject.apps.myapp.urls.myapp')),
)

So I'm using the APP_URL_ROOT defined above to define the url base
here.

In my model file, for any object that I want to have a url for an
associated detail page:

def get_absolute_url(self):
from django.conf.settings import APP_URL_ROOT
return '/%s/%d' % (APP_URL_ROOT, self.id)

Then finally, in the templates, I can do:

<a href="{{ object.get_absolute_url }}"> - which would give
http://localhost:8000/myapp/1

or <a href="{{ object.get_absolute_url }}/whatever"> - which would
give http://localhost:8000/myapp/1/whatever

Hope that gives you some ideas. I'd love to hear how others have solved
this problem.

bfor...@socialistsushi.com

unread,
Aug 17, 2005, 8:45:24 AM8/17/05
to Django users
Thanks for the suggestion. I've implemented something similar.

What I'd really like, though is things that can be updated via the
admin interface (or whatever) and that I can use in the templates:

{% if global.foo %}
....

--B

Reply all
Reply to author
Forward
0 new messages