Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

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.

Thursday, July 26, 2007

django's newforms

I've been anxiously awaiting for a long time for the newforms documentation to show up in the on line beta of the django book. It's still not there so I took matters into my own hands and did the unthinkable: I read the documentation on the django site. I know, I know, what sort of loser reads the documentation?  Desparate times call for desperate measures friends.

Anyway I think the use and function of newforms has finally settled in my brain.  Partly because I copied someones example a few months back and got it working though I didn't thoroughly understand it.  Partly because I read thru the documentation for the 3rd time. And party because I read through the unit test code for newforms.

Any way my issue was that I was trying to clone an existing object. The breakthrough was realizing that I could call form.save(commit=False) so that I could intercept the resulting model instance and set the id = None before saving it so i didn't update the original object in anyway.

I just may get the hang of this django thing one of these days...