Model for "Global Options"

6 views
Skip to first unread message

Damon Timm

unread,
Jul 4, 2009, 8:27:21 AM7/4/09
to django...@googlegroups.com
Hello everyone -

I wondered if anyone had insight on a Model for site-wide "global"
options. I am thinking of a place to store: site title, site meta,
site description, specific global links (twitter, facebook, etc),
support contact email, etc ... The model itself doesn't seem too
complicated, maybe:

class GlobalOptions(models.Model):
opiton = models.SlugField('Machine Readable Option
Name',max_length=50,unique=True)
value = models.TextField('Option Value')
# more fields could be included, of course (date_created, description, blah)

My question, though, is about how to easily get these new "options"
into a template tag. So that I could simply do:

{{ globaloptions.optionname }}

And it would return the value for that option or, if the option didn't
exist, it would return nothing. I have been looking around for a way
to do this and coming up blank -- I am new to both Python and Django,
so I'm sure the answer is there, I just don't know where/how to look
for it.

Any insight would be great!

Damon

Daniel Roseman

unread,
Jul 4, 2009, 2:02:07 PM7/4/09
to Django users
Look into the django-dbsettings project, which does just this:
http://code.google.com/p/django-values/
--
DR.

thornomad

unread,
Jul 4, 2009, 9:16:02 PM7/4/09
to Django users
That would seem to do exactly what I hoped for!

Thanks for the link. I am going to give that a shot.

thornomad

unread,
Jul 4, 2009, 9:19:37 PM7/4/09
to Django users
Actually, maybe I spoke too soon. It seems that the option still has
to be hard-coded into the models.py file initially -- that's what I
was hoping to avoid. I wanted the designer/user to just be able to
add a new key/value pair in the admin, and then access it in the
template without having to "re-code" any of the python.

On Jul 4, 2:02 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:

dvainsencher

unread,
Jul 4, 2009, 11:36:58 PM7/4/09
to Django users
You can set a globaloptions dictionary in the response context with
option attribute as key and value attribute as value.

[]s
daniel

thornomad

unread,
Jul 5, 2009, 9:12:05 AM7/5/09
to Django users
Hi Daniel -

Okay ... so, it looks like I could do this two ways (maybe more).
After reading: http://docs.djangoproject.com/en/dev/ref/templates/api/

1. I could create my own context process and define it in
TEMPLATE_CONTEXT_PROCESSORS -

2. (maybe more straightforward) I could create a function in a view
that returns a dictionary of all the items (value/key pairs) in a
particular Model ... then pass that to the view when it gets process.
Then I could add/remove value/key pairs form the admin as needed.

I think I may try out option 2, and see how it goes. Sounds easier,
anyway.

Thanks! I will report back when/if I get it working.

Damon

thornomad

unread,
Jul 5, 2009, 10:30:24 AM7/5/09
to Django users
Here is what I tried - is working. Not sure if it's as clever as it
could be, but it works.

# models.py

class GlobalOptions(models.Model):
key = models.SlugField(max_length=30,unique=True)
value = models.TextField()

# views.py

def global_options():
dict = {}
for go in GlobalOptions.objects.all():
dict[go.key] = go.value
return dict

def my_view(request, template_name):
# ...
return render_to_response('template.html', { 'global':
global_options() })

# template.html

<p>key_name = {{ global.key_name }}</p>

I can add additional keys, as needed, and just have to reference them
in the template (don't have to change any of the python).

Thoughts ?

Damon

dvainsencher

unread,
Jul 5, 2009, 8:21:40 PM7/5/09
to Django users
On Jul 5, 10:12 am, thornomad <damont...@gmail.com> wrote:
> Hi Daniel -
>
> Okay ... so, it looks like I could do this two ways (maybe more).
> After reading:http://docs.djangoproject.com/en/dev/ref/templates/api/
>
> 1. I could create my own context process and define it in
> TEMPLATE_CONTEXT_PROCESSORS -
>
> 2. (maybe more straightforward) I could create a function in a view
> that returns a dictionary of all the items (value/key pairs) in a
> particular Model ... then pass that to the view when it gets process.
> Then I could add/remove value/key pairs form the admin as needed.
>
> I think I may try out option 2, and see how it goes.  Sounds easier,
> anyway.
>

Good search, Damon.
The implementation with context processor is not so hard than in view
one.
If you want to use global anywhere i think it's a better approach.

[]s
daniel

dvainsencher

unread,
Jul 5, 2009, 8:46:38 PM7/5/09
to Django users
s/anywhere/everywhere

[]s
daniel
Reply all
Reply to author
Forward
0 new messages