为什么我必须在我的所有答复中传递RequestContext? [英] Why on earth do I have to pass RequestContext in all of my responses?

查看:168
本文介绍了为什么我必须在我的所有答复中传递RequestContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在导航菜单中突出显示当前页面。显然,当你在他们的页面上时,我需要给菜单链接一个类似活动的类。这是一个经典问题,我已经看到许多解决方案建议。我的问题是我讨厌所有的人,认为他们都不是很干涩。例如:

  @ register.simple_tag 
def active(request,pattern):
import re
如果re.search(pattern,request.path):
return'active'
return''

----

{%load tags%}
< div id =navigation>
< a class ={%active request^ / about /%}href =/ about />关于< / a>
< a class ={%active request^ / contact /%}href =/ contact />联系人< / a>
< a class ={%active request^ / services /%}href =/ services />服务< / a>
< / div>

如果您当前正在使用,该标签将接收当前的请求和一个url表达式并返回活动这一页。或者,这可以使用命名视图而不是网址完成,但原则是相同的。



我的主要问题是我的导航将在99%的我的视图,而为了获取当前的请求变量,我仍然将一个RequestContext解析到模板中,如下所示:

  def contact(request):
#snip ...
return render_to_response(
'contact.html',
{'myvar':myvar},
context_instance = RequestContext(request))

为什么我需要将这个context_instance行添加到我的每一个视图当可能除了其中一个需要请求变量以获取当前的URL /视图来突出显示活动链接?这似乎非常潮湿,特别是对于必须在绝大多数django网站的功能。我希望默认包含请求,并且可以选择性地抑制它。我在中间件中找不到一个方法,因为在视图返回之后,我无法拦截模板。



任何建议? p>

解决方案

你的意图很有意义,大部分时间你需要 RequestContext render_to_response 使用 direct_to_template 快捷方式:



django.views.generic.simple import direct_to_template

def contact(request):
#snip ...
return direct_to_template(request,'contact.html',{'myvar':myvar})

...或 render_to 装饰器从 django-annoying

  from annoying.decorators import render_to 

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


I want to highlight the current page in the navigation menu. Obviously I need to give the menu links a class like 'active' when you are on their page. This is a classic problem and I've seen many solutions proposed. My problem is I hate all of them and consider none of them to be very DRY. For example:

@register.simple_tag
def active(request, pattern):
    import re
    if re.search(pattern, request.path):
        return 'active'
    return ''

----

{% load tags %}
<div id="navigation">
    <a class="{% active request "^/about/" %}" href="/about/">About</a>
    <a class="{% active request "^/contact/" %}" href="/contact/">Contact</a>
    <a class="{% active request "^/services/" %}" href="/services/">Services</a>
</div>

The tag takes your current request and a url expression and returns 'active' if you're currently on this page. Alternatively this can be done with named views rather than urls but the principle is the same.

My main issue with this is that my navigation will be called on 99% of my views and yet, in order to get the current request variable I still have parse a RequestContext to the template with something like this:

def contact(request):
    # snip ...
    return render_to_response(
                'contact.html',
                { 'myvar' : myvar },
                context_instance=RequestContext(request))

Why do I need to add this context_instance line to every single one of my views when probably all but one of them needs the request variable in order to get the current url/view to highlight the active link? This seems awfully wet, especially for a feature that must be in the great majority of django sites. I want the request to be included by default and be able to optionally suppress it. I can't find a way to do this in middleware as I can't intercept the template before its rendered after the view has returned it.

Any suggestions?

解决方案

Your intention makes sense, you'll need RequestContext most of the time and only rarely it can be safely omitted for performance reasons. The solution is simple, instead of render_to_response use direct_to_template shortcut:

from django.views.generic.simple import direct_to_template

def contact(request):
    # snip ...
    return direct_to_template(request, 'contact.html', { 'myvar' : myvar })

... or render_to decorator from django-annoying:

from annoying.decorators import render_to

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

这篇关于为什么我必须在我的所有答复中传递RequestContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆