【问题标题】:How do I use Django's MarkUp Templates Tags with Google App Engine WebApp Framework如何将 Django 的标记模板标签与 Google App Engine WebApp 框架一起使用
【发布时间】:2011-06-25 06:42:03
【问题描述】:

我正在使用适用于 Django 模板的 Google App Engine WebApp 框架。我正在尝试使用 Django 的标记过滤器和说明说:

  • 将 django.contrib.markup 放入您的 INSTALLED_APPS
  • 通过 {% load markup %}
  • 在模板中加载标记
  • 使用适当的过滤器过滤任何文本:{{ text|textile }}

我的问题是因为我使用的是 webapp 框架,所以我没有“INSTALLED_APP”中间件。有谁知道我如何在 webapp 中加载这个模块?

【问题讨论】:

标签: google-app-engine django-templates markup


【解决方案1】:

设置标签库:

在您的应用目录中创建一个文件夹,例如 customtags

在此文件夹中创建一个空的__init__.py 文件

在同一文件夹中创建标签文件 customtags.py 例如

在“customtags.py”的开头添加以下行

from google.appengine.ext import webapp
register = webapp.template.create_template_register()

像这样将你的新标签库添加到你的 main.py 文件中:

template.register_template_library('customtags.customtags')

假设您已经拥有:

from google.appengine.ext.webapp import template 

像这样创建你的标签:

过滤标签:

@register.filter
def foobar(value):
    return value

像这样从模板调用:

{{ something|foobar }}

简单标签:

@register.simple_tag
def mysimpletag():
    print 'hello from the simple tag'

像这样从模板调用:

{% mysimpletag %}

包含标签:

@register.inclusion_tag('templates/menu.html')
def menu():
    items = db.GqlQuery('SELECT * FROM Pages')
    return {'items':items}

像这样从模板调用:

{% menu %}

【讨论】:

  • 用户没有问如何创建自己的标签,他问的是如何加载现有的库。
猜你喜欢
  • 2010-10-28
  • 2010-10-20
  • 2011-02-12
  • 2016-04-17
  • 2014-01-08
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 2015-08-12
相关资源
最近更新 更多