シンプルなミックスイン (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() の中でコンテキストを指定する便利な方法です。使用例を次に示します。

Code
from django.views.generic import TemplateView

TemplateView.as_view(extra_context={"title": "Custom Title"})

メソッド

get_context_data(**kwargs)Link to this definition

テンプレートコンテキストを表す辞書を返します。与えられたキーワード引数は、返されるコンテキストを構成します。使用例を次に示します。

Code
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_engineresponse_classusing キーワード引数として渡されます。デフォルトは None で、 Django は設定された全てのエンジンでテンプレートを検索します。

response_classLink to this definition

render_to_response メソッドが返す response クラス。 デフォルトは TemplateResponse です。 TemplateResponse インスタンスのテンプレートとコンテキストは後で変更できます (たとえば template response ミドルウェア で変更できます)。

カスタムテンプレートの読み込みやカスタムコンテキストオブジェクトのインスタンス化が必要な場合は、 TemplateResponse サブクラスを作成して response_class に代入してください。

content_typeLink to this definition

レスポンスに使用するコンテンツタイプ。 content_typeresponse_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 を含むリストを返します(指定された場合)。