チャットで多数のユーザーを返すカスタムテンプレートタグを作成しました。
{% chat_online chat_channel %}
ただし、トークンchat_channel
はその変数の値ではなく、値を受け取るようです。
どうしたの?
チャットで多数のユーザーを返すカスタムテンプレートタグを作成しました。
{% chat_online chat_channel %}
ただし、トークンchat_channel
はその変数の値ではなく、値を受け取るようです。
どうしたの?
HTMLのテンプレートタグ定義({% ... %}
)は、djangoのテンプレートエンジンによって解析される単なるテキストであるため、名前でレンダリングされているコンテキストで変数を実際に検索するようにdjangoに指示chat_channel
する必要があります。ドキュメントからのこの例は非常に明確です:
class FormatTimeNode(template.Node):
def __init__(self, date_to_be_formatted, format_string):
self.date_to_be_formatted = template.Variable(date_to_be_formatted)
...
def render(self, context):
try:
actual_date = self.date_to_be_formatted.resolve(context)
...
except template.VariableDoesNotExist:
return ''
ここで、template.Variable(date_to_be_formatted)
はテンプレートタグに渡された生の値からテンプレート変数を作成し(blog_entry.date_updated
例でself.date_to_be_formatted.resolve(context)
は)、コンテキストに対して解決することにより、テンプレート内のその変数の実際の値を見つけます。