且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django模板和变量属性

更新时间:2023-09-03 15:40:34

我发现更好/更好的解决方案,以获取变量
它不是***的方式,但它的工作。

I found a "nicer"/"better" solution for getting variables inside Its not the nicest way, but it works.

您将安装一个自定义过滤器到django它得到您的dict的关键作为参数

You install a custom filter into django which gets the key of your dict as a parameter

为了使其在谷歌应用程序引擎中工作,您需要在主目录中添加一个文件
我称之为

To make it work in google app-engine you need to add a file to your main directory, I called mine django_hack.py which contains this little piece of code

from google.appengine.ext import webapp

register = webapp.template.create_template_register()

def hash(h,key):
    if key in h:
        return h[key]
    else:
        return None

register.filter(hash)

现在我们有这个文件,我们需要做的就是告诉应用引擎使用它...
我们通过添加你的主文件的这个小线条

Now that we have this file, all we need to do is tell the app-engine to use it... we do that by adding this little line to your main file

webapp.template.register_template_library('django_hack')

并在您的模板视图中添加此模板,而不是通常的代码

and in your template view add this template instead of the usual code

{{ user|hash:item }}

=)