If you do go the RequestContext route, you can wrap the standard render_to_response function in a custom render function that adds the RequestContext automatically, as per here:
http://www.djangosnippets.org/snippets/3/ and then use the custom function in your views, instead of render_to_response. That might solve your problem... Eric On Jun 24, 2008, at 11:16 PM, Huuuze wrote: > > I understand that. However, I was hoping that the Django FRAMEWORK > would provide some easy mechanism for plucking that information out of > the session (rather than the request). Many frameworks provide easy > methods for doing so and, guessing by your response, Django does not > provide not provide this functionality. > > Having worked with my fair share of careless programmers, I'm simply > trying to do my due diligence on this topic and find an easy way to > resolve a minor issue. > > On Jun 24, 11:01 am, "Richard Dahl" <[EMAIL PROTECTED]> wrote: >> The 'core' problem as you say, is essentially the core problem of all >> programming: programs don't write themselves. You have to write >> appropriate >> code for what you are trying to do, what does it matter if someone >> forgets >> to add the username to the render call, or if someone forgets to >> add the <p> >> tags and the expected variable to the template? At some point you >> have to >> expect developers to write appropriate code. Even if there were a >> magic >> template tag that had direct access to the request object >> automatically, >> there is nothing stopping a developer from creating a new template >> and not >> using it. Personally, I would'nt spend too much more time on this >> issue, >> either use RequestContext or pass the request.user to your template >> and >> leave it at that. You could probably create a test that checked >> this and it >> may be prudent to put comments in the view or templates or a >> separate developers guide for your app to call out things like >> this. If you >> are really that concerned, you could always do something like this >> in your >> template: >> >> {% if username %} >> <p>Hello {{username}}</p> >> {% else %} >> <p>Hello Nameless One. I wanted to address you by your proper name, >> but >> unfortunately some developer forgot to pass a variable to the >> template used >> to render me! You should call and complain.<p/> >> {% endif %} >> >> :) >> -richard >> >> On 6/24/08, Huuuze <[EMAIL PROTECTED]> wrote: >> >> >> >>> Correct me if I'm wrong, but I'm not sure this resolves the core >>> problem. In your example, I still need to add something (in this >>> case, 'context_instance=RequestContext(request)') to my >>> "render_to_response" statements. I'd like to eliminate that >>> completely since it requires a developer to add something to the >>> render_to_response to get the username to render. If the developer >>> forgets to add that, then the username doesn't display. >> >>> Long story short, is there a way to get the username to render >>> using a >>> call that lives in the template and doesn't require repeated >>> references to a value in the view? In other words, if I have five >>> pages and five distinct methods in the view that each have a >>> render_to_response, how can I avoid adding five >>> 'context_instance=RequestContext(request)' values to my >>> render_to_response statements? >> >>> I apologize in advance if using RequestContext does resolve this >>> issue >>> -- still learning! >> >>> On Jun 24, 10:02 am, "Johan Liseborn" <[EMAIL PROTECTED]> >>> wrote: >>>> On Tue, Jun 24, 2008 at 15:47, Huuuze <[EMAIL PROTECTED]> wrote: >> >>>>> A n00b question for everyone: My base template has a "Welcome >>>>> <username>" section in it. Currently, I'm adding the username >>>>> (which >>>>> is coming from Django's auth/auth framework) to the template >>>>> with the >>>>> following bit of code: >> >>>>> <p>{{ request.session.user.username }}</p> >> >>>>> This works, however, it requires me to add the "request" object >>>>> to any >>>>> return statement that deals with displaying a page: >> >>>>> return render_to_response('somepage.html', {'request':request}) >> >>>>> I'm guessing there's a better way to do this, but I can't seem >>>>> to find >>>>> an answer. Help! >> >>>> I believe you can use django.template.RequestContext to accomplish >>>> what you want; in your view, do something like: >> >>>> from django.template import RequestContext >> >>>> <snip> >> >>>> return render_to_response('somepage.html', >>>> context_instance=RequestContext(request)) >> >>>> That will, among other things (and depending on the >>>> TEMPLATE_CONTEXT_PROCESSORS variable in your settings.py) >>>> populate the >>>> request-object with a "user" field. >> >>>> You can read more here: >>> http://www.djangoproject.com/documentation/templates_python/#subclass >>> ... >> >>>> Cheers, >> >>>> johan >> >>>> -- >>>> Johan Liseborn > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

