Wednesday, August 1, 2007

django messages

I was a little curious how messaging worked in django (those little messages that pop up at the top of a page after you create an object for instance), it seemed a little magical to me so I went hunting. Here's what I found:

- messages are bound up with a logged in user. they actually have their own class: django.contrib.auth.models.Message with a FK to users.

- messages get added to the user directly: (e.g. this was taken from
create_update.create_object)
request.user.message_set.create(message=ugettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name})

- the very next time a page is rendered "messages" will be available as a variable in the template. use it or lose it. it will be consumed whether you display it or not.

- part of the "magic" (to me) was where this was coming from. global_settings.TEMPLATE_CONTEXT_PROCESSORS has 'django.core.context_processors.auth'

- when RequestContext is instantiated for a request to be used in a template it loops over the items in TEMPLATE_CONTEXT_PROCESSORS one of which is the function auth from above which returns a dictionary of 'user', 'messages', 'perms'

Not so magical now, but interesting.

No comments: