简体   繁体   English

Django 1.1相当于'in'运算符

[英]Django 1.1 equivalent of the 'in' operator

I need to display a piece of HTML only if a variable value appears in a list. 只有当变量值出现在列表中时,我才需要显示一段HTML。 I know that Django 1.2 has an 'in' operator. 我知道Django 1.2有一个'in'运算符。 But I am working on a Google App Engine app. 但我正在开发Google App Engine应用程序。 Is there a workaround I can use? 我有可以使用的解决方法吗?

You can use your own template tag to achieve it or put it in your controller's logic. 您可以使用自己的模板标记来实现它,也可以将它放在控制器的逻辑中。

Have a look at this snippet: http://www.djangosnippets.org/snippets/302/ 看看这个片段: http//www.djangosnippets.org/snippets/302/

If what you need to know is whether you should render a piece of HTML, and you are going to reuse this rule in other templates, you may try to use django.template.RequestContext and make it an accessable status variable in templates in need. 如果您需要知道的是是否应该呈现一段HTML,并且您要在其他模板中重用此规则,则可以尝试使用django.template.RequestContext并使其成为需要的模板中的可访问状态变量。

def context(request):
    return {'render_a_panel' : request.user.username in ('Jim', 'Tom')}

Of course, this only works if your rule is based on request. 当然,这仅适用于您的规则基于请求的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM