シンプルなミックスイン (mixin)Link to this heading
ContextMixinLink to this heading
- class django.views.generic.base.ContextMixinLink to this definition
属性
- extra_contextLink to this definition
コンテキストに含める辞書。これは、
as_view()の中でコンテキストを指定する便利な方法です。使用例を次に示します。from django.views.generic import TemplateView TemplateView.as_view(extra_context={"title": "Custom Title"})
メソッド
- get_context_data(**kwargs)Link to this definition
テンプレートコンテキストを表す辞書を返します。与えられたキーワード引数は、返されるコンテキストを構成します。使用例を次に示します。
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["number"] = random.randrange(1, 100) return contextすべてのクラスベースのジェネリックビューのテンプレートコンテキストには、
Viewインスタンスを指すview変数が含まれています。
TemplateResponseMixinLink to this heading
- class django.views.generic.base.TemplateResponseMixinLink to this definition
適切なコンテキストを指定して
TemplateResponseを構築するメカニズムを提供します。使用するテンプレートは設定可能で、サブクラス化によりさらにカスタマイズできます。属性
- template_nameLink to this definition
文字列で指定された使用するテンプレートの完全な名前。
template_nameを定義しないと、django.core.exceptions.ImproperlyConfigured例外が発生します。
- template_engineLink to this definition
テンプレートを読み込む際に使用するテンプレートエンジンの
NAMEです。template_engineはresponse_classのusingキーワード引数として渡されます。デフォルトはNoneで、 Django は設定された全てのエンジンでテンプレートを検索します。
- response_classLink to this definition
render_to_responseメソッドが返す response クラス。 デフォルトはTemplateResponseです。TemplateResponseインスタンスのテンプレートとコンテキストは後で変更できます (たとえば template response ミドルウェア で変更できます)。カスタムテンプレートの読み込みやカスタムコンテキストオブジェクトのインスタンス化が必要な場合は、
TemplateResponseサブクラスを作成してresponse_classに代入してください。
- content_typeLink to this definition
レスポンスに使用するコンテンツタイプ。
content_typeはresponse_classのキーワード引数として渡されます。デフォルトはNoneで、その場合 Django は'text/html'を使用します。
メソッド
- render_to_response(context, **response_kwargs)Link to this definition
self.response_classインスタンスを返します。キーワード引数を指定すると、response クラスのコンストラクタに渡されます。
get_template_names()を呼び出すと、存在するテンプレートを探すために検索されるテンプレート名のリストを取得できます。
- get_template_names()Link to this definition
テンプレートをレンダリングする際に検索するテンプレート名のリストを返します。最初に見つかったテンプレートが使用されます。
デフォルトの実装では、
template_nameを含むリストを返します(指定された場合)。