我將一組模板標籤添加到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