简体   繁体   English

使用Google App Engine Python 2.7在Django 1.2中自定义标签

[英]Custom Tags in Django 1.2 with Google App Engine Python 2.7

Creating a custom tag in Google App Engine Python2.5 with Webapp used to be a joyful experience. 使用Webapp在Google App Engine Python2.5中创建自定义标记曾经是一种快乐的体验。 Here: Django templates and variable attributes 这里: Django模板和变量属性

But now, in Python 2.7 with Webapp2 and Django 1.2, it is a pain in the ass. 但是现在,在带有Webapp2和Django 1.2的Python 2.7中,它是一个痛苦的屁股。 I can only find bits of information here and there, and some of the methods contradict with each other. 我只能在这里和那里找到一些信息,而且有些方法相互矛盾。

The method described in http://www.john-smith.me/Tag/webapp2 ranks high in Google, but some people claim it is "what a waste of time" Webapp2 custom tags http://www.john-smith.me/Tag/webapp2中描述的方法在Google中排名很高,但有些人声称这是“浪费时间” Webapp2自定义标签

This one method seems to work 这种方法似乎有效

from django.template.loader import add_to_builtins
add_to_builtins('xxxxx')

But I dont know the details. 但我不知道细节。 Who can provide a step by step example? 谁可以提供一步一步的例子?

I dont know why there are no official documents about these stuff. 我不知道为什么没有关于这些东西的官方文件。 I mean, this is not science experiments in which we explore the unknown. 我的意思是,这不是我们探索未知的科学实验。 There supposed to be some documentation so the developers can save their time. 应该有一些文档,以便开发人员可以节省时间。

I had same problem. 我有同样的问题。

The fix: 修复:

  1. Create templatetags folder. 创建templatetags文件夹。 Add there module with custom tags as explained in Django docs Custom template tags and filters . 使用自定义标记添加模块,如Django文档自定义模板标记和过滤器中所述 Example folder structure: 示例文件夹结构:

     app.yaml myapp/ __init__.py templatetags/ __init__.py my_tags.py 
  2. In settings.py set INSTALLED_APPS to myapp (name of folder which contains templatetags sub-folder): 在settings.py中将INSTALLED_APPS设置为myapp(包含templatetags子文件夹的文件夹名称):

     INSTALLED_APPS = ( 'myapp' ) 
  3. Now when you call {% load my_tags %} in template Django should also seek the mytags module in myapp/*/templatetags/ folder. 现在,当你在模板中调用{% load my_tags %} ,Django也应该在myapp/*/templatetags/文件夹中寻找mytags模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM