Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Google应用程序引擎上Django中的包含标记_Python_Django_Google App Engine_Django Templates - Fatal编程技术网

Python Google应用程序引擎上Django中的包含标记

Python Google应用程序引擎上Django中的包含标记,python,django,google-app-engine,django-templates,Python,Django,Google App Engine,Django Templates,我想创建一个可重用的模板,就像.NET世界中的UserControl一样,可以在多个地方应用,比如: {% for thing in things %} {% render_thing thing %} {% endfor %} 其中render_东西是我的自定义包含标记。我的Python代码如下所示: def get_full_path(relative_path): return os.path.join(os.path.dirname(__file__), relativ

我想创建一个可重用的模板,就像.NET世界中的UserControl一样,可以在多个地方应用,比如:

{% for thing in things %}
    {% render_thing thing %}
{% endfor %}
其中render_东西是我的自定义包含标记。我的Python代码如下所示:

def get_full_path(relative_path):
    return os.path.join(os.path.dirname(__file__), relative_path)

def render_thing(thing):
    return {'thing':thing }

register = template.create_template_register()
register.inclusion_tag(get_full_path('thing.html'))(render_thing)
其中thing.html是我的小模板。但是,当我运行此命令时,会出现以下错误:

TemplateSyntaxError: Invalid block tag: 'render_thing'

我缺少什么?

如果您使用的是Django 1.2模板,则需要为自定义标记代码提供Python模块样式的引用,而不是文件路径

有一个

编辑: 很抱歉对你这么高层次。这里有一个更详细的解释:

将自定义标记代码放在一个文件中,例如my_custom_tags.py

以自定义标记代码所在的.py文件为例,将其放在主AppEngine项目目录的子目录中,例如customtags

在新的子目录中,创建一个名为uu init_uu.py的空文件 在AppEngine应用程序的main.py文件中,添加以下代码:

从google.appengine.ext.webapp导入模板 模板。注册\u模板\u库'customtags.my\u custom\u tags'


在自定义标记库中定义的所有自定义标记现在都可以在模板文件中使用,无需额外工作。

您需要在使用它的每个模板中加载templatetag库

{% load my_template_library %}
加载模板标签

{% load my_template_library %}
参见手册


您使用的是什么版本的Django?AppEngine上当前的默认值非常旧-0.96。对,我尝试添加use_库'django','1.2'代码,但它错误地说它无法识别use_库:哦,忘记导入了。有趣的是,修复了这个问题-现在有一个新的错误TemplateSyntaxError:无效的块标记:“render_activity”,应该是“empty”或“endfor”。嗨,Adam,恐怕这没有什么帮助。它可能对Python/GAE忍者有所帮助,但我真的不知道我需要做什么才能开始。@ConfusedNoob;我添加了更明确的步骤。希望能有帮助。我自己设法破解了它,但我认为你的回答对我这种情况下的其他人会很有帮助,谢谢!如果您遵循Adam概述的步骤,那么这实际上是没有必要的。