我将一组模板标签添加到Django应用程序中,我不知道如何测试它们。我在模板中使用过它们,它们似乎在工作,但我正在寻找更正式的东西。主要逻辑在模型/模型管理器中完成并且已经过测试。该标签只检索数据,并将其存储在作为如何在Django中测试自定义模板标签?
{% views_for_object widget as views %}
"""
Retrieves the number of views and stores them in a context variable.
"""
# or
{% most_viewed_for_model main.model_name as viewed_models %}
"""
Retrieves the ViewTrackers for the most viewed instances of the given model.
"""
所以我的问题是,你通常测试的模板标签,如果你这样做,你怎么办了一个环境变量这样?
如何在项目范围之外测试这种情况,比如说,对于可重用应用程序?渲染包含“{%load custom_tag%}”的模板字符串似乎不起作用,至少没有额外的工作? – Santa 2010-04-23 19:40:37
回答了我自己的问题:使用'register = template.Library(); template.libraries ['django.templatetags.mytest'] = register; register.tag(name ='custom_tag',compile_function = custom_tag)'。 – Santa 2010-04-23 20:15:10
代码示例中的“self”是什么? '我的TestCase'对象没有'render_template'方法 – 2015-01-25 13:37:19