django模板上的动态名称变量 [英] Dynamic name variable on django template

查看:327
本文介绍了django模板上的动态名称变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django的问题。我有一个dict与我的所有网站文本的翻译。例如:

  term = {level_1:Noob,
level_2:Noob 2

问题是,如何在django模板上访问此密钥?



我有

  img src =/ images / level _ {{player.level.id }}。jpg
title ={{term.level ???????}}

我试过:

  title ={{term.level {{player.level.id}} 
$ / code

但这当然不行。

解决方案

Django的模板语言是(设计上的)很愚蠢/限制,在评论中,Davind Wolever指出:在Django模板中使用变量访问dict?,其中回答建议制作自定义模板标签。



我认为在你的情况下,最好在你的视图代码中处理它,而不是只将播放器传递到您的上下文中,同时传递级别ID和级别名称。



您甚至可以直接传递图像网址和级别名称?不在您的模板中构建URL,使其更易于阅读。


I'm having a problem with django. I have a dict with all my site texts for translations. For example:

term = {"level_1": "Noob",
        "level_2": "Noob 2"}

The problem is, how can I access this key on django template?

I have

img src="/images/level_{{player.level.id}}.jpg" 
    title="{{term.level??????? }}"

I tried:

title="{{term.level{{player.level.id}}}} 

but of course this didn't work.

解决方案

Django's template language is (by design) pretty dumb/restricted. In his comment, Davind Wolever points at Accessing a dict by variable in Django templates?, where an answer suggests to make a custom template tag.

I think that in your case, it is best to handle it in your view code. Instead of only passing along a player into your context, pass both the level ID and the level name.

Possibly you can even directly pass the image url and the level name? Not constructing the URL in your template makes it more readable.

这篇关于django模板上的动态名称变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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