I'm builing a wedding website creator (no judgement please).
Almost every view needs to call a Wedding.objects.get(id=wedding_id)
and then pass it to the template as part of the variables.
Seems like this is a good use for a custom context processor. Wondering what the best way to create a context processor that would read the URL and if there was a wedding ID, include a wedding object in the template. If there wasn't a wedding ID, then wedding=None in the template.
First, I would make sure you really need this for almost every request, since you would be coding in an explicit query to each request. A
get
query is immediate as opposed to a lazyfilter
. If you wanted to make it more lazy you could wrap the get in a small "get_wedding" wrapper, or use afilter
and just grab it from the list...If you want to try a lazy approach, so that the query only gets run if you actually use the object, you can wrap it in a lazy object: