django - Why on earth do I have to pass RequestContext in all of my responses? -


I want to highlight the current page in the navigation menu. Obviously, I need to give the link to a class like 'active' when you are on their page. This is a classic problem and I have seen many solutions. My problem is that I hate them all and none of them think of being very dry. For example:

  @ register.simple_tag def active (request, pattern): import re if re.search (pattern, request.path): return 'active' return '' --- - {% load tag%} & lt; Div id = "navigation" & gt; & Lt; A class = "{% active request" ^ / about / "%}" href = "/ about /" & gt; About & lt; / A & gt; & Lt; A class = "{% active request" ^ / contact / "%}" href = "/ contact /" & gt; Contact & lt; / A & gt; & Lt; A class = "{% active request" ^ / services / "%}" href = "/ services /" & gt; Service & lt; / A & gt; & Lt; / Div & gt; The  

tag takes your current request and a URL expression and 'active' if you are active on this page. Alternatively, this can be done with named scenes instead of URL, but the principle is the same.

My main point is that my navigation is presenting 99% of my thoughts and so far, the current request variable is still requestContext for templates with something like this:

< Pre> def contact (request): # snip ... return render_to_response ('contact.html', {

I need to add this reference_instance line to each of my views Why is it possible for all but one of them to highlight the active link The demand variables are required to get the value URL / view This sounds awful, especially for a feature that should be in the great majority of the DjZo sites. I want to request to join in by default I want to be able to suppress it alternatively. I do not find any way to do this in middleware because after touching it after touching it

Any suggestions?

Your intentions are understandable , You will need the most time code RequestContext and only rarely can it be left safely for display reasons. The solution is simple, use render_to_response direct_to_template shortcut: django.views.generic.simple import direct_to_template def contact (request from

  ): #kick ... Return Direct_t_template (request, 'contact.html', {'myvar': myvar})  

... or rendere_to decorator Import: render_to@render_to ('template.html') def foo (request): bar = Bar.object.all () return {'bar': bar} from:

annoying.decorators

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -