... myapp/index.html をMIMEタイプ application/xhtml+xml でレンダリングしてみます。 from django.shortcuts import render def my_view ( request ): # View code here... return render ( request , "myapp/index.html" , { "foo" : "bar" , }, content_type = "appli ... tml+xml" , ) この例は次のコードと等価です。 from django.http import HttpResponse from django.template import loader def my_view ( request ): # View code here... t = loader . get_template ( "myapp/index.html" ) c = { "foo" : "bar" } return HttpRespons ... ェクトの get_absolute_url() メソッドが呼び出され、リダイレクト URL を返します: from django.shortcuts import redirect def my_view ( request ): ... obj = MyModel . objects . get ( ... ) return redirect ( obj ) ビューの名前と、オプションで位置引数またはキ ... ... 原型のクラスは 継承元 (MRO) セクションにドキュメントがあります。MRO は メソッド解決順序 (Method Resolution Order) の略です。 View class django.views.generic.base. View 基本ビュークラス。他の全てのクラスベースのビューはこの基底クラスを継承します。厳密に ... はジェネリックビューではないので、 django.views からインポートすることもできます。 メソッドのフローチャート setup() dispatch() http_method_not_allowed() options() Exampl ... e views.py : from django.http import HttpResponse from django.views import View class MyView ( View ): def get ( self , request , ... ... 構造化し、継承やミックスインを活用してコードを再利用できます。また、汎用ビュー(generic view)も用意されていますが、それについては後ほど説明します。あなたのユースケースに合った再利用可能なビューの構造を設計することも ... ミックスイン (mixin) を使用する There are community-maintained solutions! Django has a vibrant ecosystem. There are generic view extensions highlighted on the Community Ecosystem page that build on Django's class-based views. The Django Packages Class ... Based Views grid has even more options for you! 基本的な例 Django は、さまざまなアプリケーションに広く適用できるベースビュークラスを用 ... ... 、ビュー関数の場合に HTTP GET をハンドリングするコードが次のようになるとすると、 from django.http import HttpResponse def my_view ( request ): if request . method == "GET" : # <view logic> return HttpResponse ( "result" ) クラスベースのビューでは以下の ... ようになります。 from django.http import HttpResponse from django.views import View class MyView ( View ): def get ( self , request ): # <view logic> return HttpResponse ( "result" ) DjangoのUR ... 、クラスベースのビューには、関連するパターンに一致するURLに対するリクエストが到着したときに呼び出すことができる関数を返す as_view() クラスメソッドがあります。この関数はクラスのインスタンスを作成し、 setup() を呼び出してその属性を初期化し、次にその disp ... ... リファレンス文書は、 クラスベースビュー を参照してください。 参考 Classy Class-Based Views では、ビルトインのクラスベースビューのクラスのヒエラルキーがわかりやすく紹介されています。 シンプルなジェネリックビュー V ... iew class View 属性 (とオプションのアクセサ): http_method_names メソッド as_view() dispatch() head() http_method_not_allowed() setup() Te ... mplateView class TemplateView 属性 (とオプションのアクセサ): content_type extra_context http_method_names response_class [ render_to_ ... ... もできます: from django.contrib import admin class AuthorAdmin ( admin . ModelAdmin ): list_display = [ "name" , "title" , "view_birth_date" ] @admin . display ( empty_value = "???" ) def view_birth_date ( self , obj ): return obj . birth_date ModelAd ... は、フィールドレベルで以下のようにできます。 class PersonAdmin ( admin . ModelAdmin ): list_display = [ "name" , "birth_date_view" ] @admin . display ( empty_value = "unknown" ) def birth_date_view ( self , obj ): return obj . birth_date 与えられた文字 ... 可されていないデータが漏洩されるのを防ぐため、オートコンプリートを使うには、ユーザーは必ずリレーション先オブジェクトに対する view か change 権限を持っていなければなりません。 結果のソートおよびページ分割は、関係する ModelAdmin の get_ordering() と get_ ... ... 機能もあるかもしれません。たとえば、HTTP レスポンスを生成するテンプレートをレンダリングするビューを記述したいとき、 TemplateView は使えない状況もあります; POST ではテンプレートをレンダリングするだけで、 GET のときはまったく異なる処理がしたいときなどで ... スベースビュー内のテンプレートを扱うインターフェースに一貫性を保ちやすくなっています。 TemplateResponseMixin Every built in view which returns a TemplateResponse will call the render_to_response() method that TemplateResponseMixin provides. Most of th ... e time this will be called for you (for instance, it is called by the get() method implemented by both TemplateView and DetailView ); similarly, it's unlikely that you'll need to override it, although if you want your response to return s ... ... explanation in Nonce usage . デコレータ Django provides decorators to control the Content Security Policy headers on a per-view basis. These allow overriding or disabling the enforced or report-only policy for specific views, providing fine-grained c ... n" policy, an attacker could exploit a vulnerability on one page to access other parts of the site. csp_override ( config)(view ) [ソース] Overrides the Content-Security-Policy header for the decorated view using directives in the same format as the ... directives. If config is an empty mapping ( {} ), no CSP enforcement header will be added to the response returned by that view, effectively disabling CSP for that view. 例: from django.http import HttpResponse from django.utils.csp import CSP from d ... ... "main-view" ), path ( "bio/<username>/" , views . bio , name = "bio" ), path ( "articles/<slug:title>/" , views . article , name = "a ... rticle-detail" ), path ( "articles/<slug:title>/<int:section>/" , views . section , name = "article-section" ), path ( "blog/" , include ( "blog.urls" )), ... , ] route The route argument shoul ... ing may contain angle brackets (like <username> above) to capture part of the URL and send it as a keyword argument to the view. The angle brackets may include a converter specification (like the int part of <int:section> ) which limits the character ... ... isplaying a parent model with related model inlines, where the user has view-only permissions to a parent model but edit permissions to the inline model, would display a read-only view of the parent ... olve this issue, the permission handling code of the Django admin interface has been changed. Now, if a user has only the "view" permission for a parent model, the entire displayed form will not be editable, even if the user has permission to edit mo ... ware that some users of Django were depending on the ability to allow editing of inlines in the admin form of an otherwise view-only parent model. Given the complexity of the Django admin, and in-particular the permissions related checks, it is the v ... 2026年10月08日