... head() http_method_not_allowed() setup() TemplateView class TemplateView 属性 (とオプションのアクセサ): content_type extra_context http_method_names response_class [ render_to_response() ] template_engine template_name [ get_template_names() ] メソッド ... as_view() dispatch() get() get_context_data() head() http_method_not_allowed() render_to_response() setup() RedirectView class RedirectView 属性 (とオプションのア ... llowed() options() post() put() setup() 詳細ビュー DetailView class DetailView 属性 (とオプションのアクセサ): content_type context_object_name [ get_context_object_name() ] extra_context http_method_names model pk_url_kwarg query_pk_and_slug queryset [ ... ... ムを使うには3つのステップがあります: Engine を設定します。 テンプレートコードを Template にコンパイルします。 テンプレートを Context においてレンダリングします。 これらの各ステップで、Django プロジェクトは通常、テンプレートシステムの低レベルの API ではなく ... mplate を返します。 前のステップで取得した Template には render() メソッドがあり、コンテキストと場合によってはリクエストを Context に集約し、その下にある Template にレンダリングを委譲します。 エンジンを設定する もし DjangoTemplates バックエンドを使ってい ... 後述する属性のデフォルトは DjangoTemplates で渡されたもので上書きされます。 class Engine ( dirs = None , app_dirs = False , context_processors = None , debug = False , loaders = None , string_if_invalid = '' , file_charset = 'utf-8' , libraries = None , ... ... mat_html() を使うことを強く推奨します。 テンプレートタグの中からコンテキストにアクセスしたい場合、タグを登録する際に takes_context 引数を使うことでできるようになります。 @register . simple_tag ( takes_context = True ) def current_time ( context , format ... _string ): timezone = context [ "timezone" ] return your_get_current_time_method ( timezone , format_string ) 最初の引数は context に しなければならない ... ことに注意してください。 takes_context オプションがどのように動くかについて、詳しくは inclusion tag を参照してください。 タグの名前を変更する必要がある場合は、カ ... ... mplateResponseMixin ) は、実際のオブジェクトを扱うときに、より柔軟なデフォルトを提供するためにこれをオーバーライドします。 ContextMixin Every built in view which needs context data, such as for rendering a template (including TemplateResponseMixin above ... ), should call get_context_data() passing any data they want to ensure is in there as keyword arguments. get_context_data() returns a dictionary; in ... its keyword arguments, but it is common to override this to add more members to the dictionary. You can also use the extra_context attribute. Django の一般的なクラスベースのビューを構築する Djangoのクラスベースのジェネリックビューが、個々の機能を提供する ... ... チャート setup() dispatch() http_method_not_allowed() get_template_names() get_slug_field() get_queryset() get_object() get_context_object_name() get_context_data() get() render_to_response() 例 myapp/views.py : from django.utils import timezone from dja ... tail import DetailView from articles.models import Article class ArticleDetailView ( DetailView ): model = Article def get_context_data ( self , ** kwargs ): context = super () . get_context_data ( ** kwargs ) context [ "now" ] = timezone . now () retur ... n context 例 myapp/urls.py : from django.urls import path from article.views import ArticleDetailView urlpatterns = [ path ( "<slug: ... ... ull count of objects should be displayed on a filtered admin page. The AdminSite.password_change() method now has an extra_context parameter. You can now control who may login to the admin site by overriding only AdminSite.has_permission() and AdminSite ... .login_form . The base.html template has a new block usertools which contains the user-specific header. A new context variable has_permission , which gets its value from has_permission() , indicates whether the user may access the site. For ... e will now include a cache busting clause when getting the description of a table. E メール Email backends now support the context manager protocol for opening and closing connections. The SMTP email backend now supports keyfile and certfile authenticat ... ... かし、この名前でも良くないと感じるなら、コンテキストの変数名を手動で設定することもできます。次のように、ジェネリックビューの context_object_name 属性を設定すると、コンテキスト変数の名前として使えるようになります。 # views.py from django.views.generic impo ... rt ListView from books.models import Publisher class PublisherListView ( ListView ): model = Publisher context_object_name = "my_favorite_publishers" 分かりやすい context_object_name を設定するのはいつでも良い考えです。テンプレートの ... すが、テンプレートに必要な他の追加情報を使うにはどうすれば良いのでしょうか? その答えは、 DetailView をサブクラス化して、 get_context_data メソッドを自分で実装することです。デフォルトの実装では、単にテンプレートで表示されるオブジェクトだけを追加しますが、こ ... ... ビューでこのように使用します: from django.db import connection from django.shortcuts import render def my_view ( request ): context = { ... } # Code to generate context with all data. template_name = ... with connection . execute_wrapper ( blocker ): ret ... urn render ( request , template_name , context ) ラッパーに送られるパラメーターは以下の通りです: execute -- クエリを実行するために、残りのパラメータと一緒に呼び出される呼 ... te() なのか executemany() なのかを示す bool ( params が値のシーケンスなのか、値のシーケンスのシーケンスなのかを示します)。 context -- 呼び出しのコンテキストに関するデータを含む辞書。これにはコネクションとカーソルが含まれます。 このパラメータを使用するこ ... ... eneric import TemplateView TemplateView . as_view ( extra_context = { "title" : "Custom Title" }) メソッド get_context_data ( ** kwargs ) テンプレートコンテキストを表す辞書を返します。与え ... られたキーワード引数は、返されるコンテキストを構成します。使用例を次に示します。 def get_context_data ( self , ** kwargs ): context = super () . get_context_data ( ** kwargs ) context [ "number" ] = random . randrange ( ... 1 , 100 ) return context すべてのクラスベースのジェネリックビューのテンプレートコンテキストには、 View インスタンスを指す view 変数が含まれています ... ... ntinue to use the base unittest library, you can -- you just won't get any of the nice new unittest2 features. Transaction context managers Users of Python 2.5 and above may now use transaction management functions as context managers. For example: with ... de set null, set default, set to any value, protect, or do nothing. For more information, see the on_delete documentation. Contextual markers and comments for translatable strings For translation strings with ambiguous meaning, you can now use the pgett ... ext function to specify the context of the string. And if you just want to add some information for translators, you can also add special translator comments ... 2026年10月08日