Perfect!

I read about the context processors before and that's why I originally
opted to use RequestContext (if only for the 'user' variable being
accessible everytime). Adding my own context processor for this
particular case hadn't cross my mind. Everything now works exactly as
I would like it to.

Thanks again,

- Eric

On Jun 9, 4:35 am, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
> On Jun 9, 5:23 am, EricR86 <notthebmovieac...@gmail.com> wrote:
>
>
>
> > Hello,
>
> > I have a small issue in terms callable objects inside of contexts. I'm
> > having trouble figuring out how to have the call evaluated everytime
> > the context is used/rendered. An example:
>
> > some_context = {
> >     'random': random(),
>
> > }
>
> > def some_view(request):
> >     return render_to_response('some_template.html',
> >                {},
> >                context_instance=RequestContext(request, some_context)
> >            )
>
> > Now as far as I can figure, the 'random' variable used in the template
> > will be the same regardless of how many times some_view is called. Not
> > only that, but all instances of RequestContext that use some_context
> > will also have the same value of 'random'. I can't just pass in the
> > random function directly (without the brackets) since it just prints
> > garbadelygoob about being function. This is a little counterintuitive,
> > aren't object members checked if they're callable and evaluated, but
> > not functions themselves?
>
> > In my particular instance I have a function that returns a dynamically
> > changing result. This result is used in all pages on my website and I
> > would ideally like it to have evaluated everytime the view is called.
>
> > The current quick hack/workaround is to simply use 'random': random()
> > everywhere I make a call to render_to_response (and not have it stored
> > in the RequestContext / some_context portion). This guarantees that
> > 'random' will be a different value each time the view is called. But
> > this is less than ideal and I was wondering if there was less of a
> > hack to solve this problem.
>
> > Thanks for your time,
>
> > - Eric
>
> Putting the context definition at module level will definitely cause
> it to be executed once only, when the module is first imported. That's
> not a Django limitation, that's how Python works.
>
> I can't help with why passing the function object directly doesn't
> work, except to suggest trying {{ random.__call__ }} in the template.
>
> However, it seems to me that your requirement is an idea fit for a
> context processor. Make the function return a dictionary, add it to
> your CONTEXT_PROCESSORS setting, and you're done - you're already
> using RequestContext in your render_to_response view.
>
> See:http://docs.djangoproject.com/en/dev/ref/templates/api/#id1
> (confusingly found under 'Template API' in the table of contents).
> --
> DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to