私は初心者の django プログラマーで、Django テンプレート用の独自のテンプレート タグを作成したいと考えています。templatetags モジュールを作成しましたが、表示されているコードを使用すると正しく動作するようです。"<"
ただし、私の関数はand">"
の代わりに"<"
andを含む文字列を返します">"
(関数の結果が関数によって変更されたかのようにaddslashes()
)。コードの何が問題になっていますか?
base_template.html (私のテンプレート タグを使用する django テンプレート)
<% load templatetags %>
<html>
<head>
</head>
<body>
{# text contains a string #}
{{ text | formattedtext }}
</body>
</html>
templatetags.py
from django import template
register = template.Library()
@register.filter(name='formattedtext')
def formattedtext(value):
try:
scoringTemplate = "<b>" + value + "</b>"
print scoringTemplate #return string with "<b>text</b>"
return scoringTemplate #however, this returns string with "<text>" value :(
except ValueError:
return value
except:
return value