English 中文(简体)
如何将Django的MarkUp模板标签与谷歌应用引擎WebApp框架一起使用
原标题:How do I use Django s MarkUp Templates Tags with Google App Engine WebApp Framework

我使用的是谷歌应用引擎WebApp框架,它与Django的模板配合使用。我正在尝试使用Django的MarkUp过滤器,说明如下:

  • Put django.contrib.markup in your INSTALLED_APPS
  • Load markup in your templates via {% load markup %}
  • Filter whatever text with the appropriate filter: {{ text|textile }}

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

问题回答

Set up tag library:

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

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

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

在customtages.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 

Create your tags like so:

筛选器标记:

@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}

来自templte的电话是这样的:

{% menu %}




相关问题
How to make logging.debug work on Appengine?

I m having a tough time getting the logging on Appengine working. the statement import logging is flagged as an unrecognized import in my PyDev Appengine project. I suspected that this was just an ...

gqlQuery returns object, want list of keys

Is there a way to convert the GqlQuery object to an array of keys, or is there a way to force the query to return an array of keys? For example: items = db.GqlQuery("SELECT __key__ FROM Items") ...

Integrating Google AppEngine with a Thick Client

I want to make a multi-user client-server solution with Java Swing thick client as a front-end and Google AppEngine (Java one) as a back-end. The problem is that GAE provides only web-based forms for ...

sorl.thumbnail : thumbnail is not a valid tag library?

I am trying to install sorl.thumbnail but am getting the following error message: thumbnail is not a valid tag library: Could not load template library from django.templatetags.thumbnail, No module ...

热门标签