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

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

问题描述

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

@register.simple_tag定义活动(请求,模式):进口重新如果 re.search(pattern, request.path):返回活跃"返回 '​​'----{% 加载标签 %}<div id="导航"><a class="{% active request "^/about/" %}" href="/about/">关于</a><a class="{% active request "^/contact/" %}" href="/contact/">联系方式</a><a class="{% active request "^/services/" %}" href="/services/">Services</a>

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

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

def 联系(请求):#剪...返回 render_to_response('contact.html',{'myvar':myvar},context_instance=RequestContext(request))

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

有什么建议吗?

解决方案

您的意图是有道理的,您在大多数情况下都需要 RequestContext 并且很少出于性能原因可以安全地省略它.解决方案很简单,而不是 render_to_response 使用 direct_to_template 快捷方式:

 from django.views.generic.simple import direct_to_template定义联系(请求):#剪...返回 direct_to_template(请求,'contact.html',{'myvar':myvar })

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

from 烦人的.decorators 导入 render_to@render_to('template.html')def foo(请求):bar = Bar.object.all()返回 {'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天全站免登陆