On Friday, January 7, 2011 5:19:10 PM UTC, Joe0418 wrote: > > > Greetings! > > I've been spending the past few weeks learning django and love it! I'm > running into a snag though... > > I'm using just one template for all my toying right now, and it has a few > sections in it that looks something like this: > > {% if user.username %} > <center><h3>Welcome {{ user.username }}!</h3></center> > {% else %} > <center> > <h3>Welcome anonymous user!</h3> > <h2>Please /login/ login </h2> > </center> > {% endif %} > > -- and -- > > {% for link in link_list %} > {{ link.href }} {{ link.name }} > {% if not forloop.last %} > | > {% endif %} > {% endfor %} > > > I have around a dozen views, and I'm noticing that in each of my views I'm > having to do something like: > > return render_to_response('base.html', { #various logic > 'link_list': link_list, > 'user': request.user }) > > I have link_list defined in another file, so each function can handle that > properly. The problem creeps up on the 'user': request.user logic though. > Instead of having to have this line of code in EVERY return statement, I'd > rather just define it once in a dictionary at the top of my views.py page > or > in another file, with something like this: > > dictionary_of_variables = {'user': request.user, 'link_list': link_list, > #various other global things} > > I'd also like to just return something like this in each of my functions: > > business_logic_variable = "some business logic" > dictionary_of_variables['template_variable'] = business_logic_variable > return render_to_response('base.html', dictionary_of_variables) > > > HOWEVER: I can't do that because request.user is out of the scope of a > 'request' that you would typically pass to a view function. My specific > error is: > > name 'request' is not defined > > > Is there a workaround for this sort of logic? I don't mind returning it in > my 10 or so functions that I have right now, but when my webpage grows to > hundreds of functions & pages, it would be much better to globally define > this variable. > > Thanks for your reply! >
Context processors are what you need. http://docs.djangoproject.com/en/1.2/ref/templates/api/#subclassing-context-requestcontext -- 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-us...@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.