0

チャットで多数のユーザーを返すカスタムテンプレートタグを作成しました。

{% chat_online chat_channel %}

ただし、トークンchat_channelはその変数の値ではなく、値を受け取るようです。

どうしたの?

4

1 に答える 1

2

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)は)、コンテキストに対して解決することにより、テンプレート内のその変数の実際の値を見つけます。

于 2012-04-24T11:05:19.763 に答える