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

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

问题描述

我想在导航菜单中突出显示当前页面.显然,当您在他们的页面上时,我需要给菜单链接一个像活动"这样的类.这是一个经典问题,我见过很多提出的解决方案.我的问题是我讨厌所有这些,并且认为它们都不是很干燥.例如:

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>

如果您当前在此页面上,该标签将获取您当前的请求和一个 url 表达式并返回活动".或者,这可以使用命名视图而不是 url 来完成,但原理是一样的.

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.

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

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))

为什么我需要将这个 context_instance 行添加到我的每一个视图中,而其中可能除了其中一个之外需要请求变量以获取当前 url/view 以突出显示活动关联?这似乎非常潮湿,尤其是对于必须在绝大多数 django 站点中的功能.我希望默认情况下包含该请求,并且可以选择禁止它.我找不到在中间件中执行此操作的方法,因为在视图返回模板后,我无法在模板呈现之前拦截它.

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.

有什么建议吗?

推荐答案

你的意图是有道理的,你大部分时间都需要 RequestContext 并且很少出于性能原因可以安全地省略它.解决方法很简单,代替 render_to_response 使用 direct_to_template 快捷方式:

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 })

... 或 render_to 装饰器来自 django-annoying:

... 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天全站免登陆