... ted like other expressions . 条件式クラス このページの例では、以下のモデルを使用します: from django.db import models class Client ( models . Model ): REGULAR = "R" GOLD = "G" PLATINUM = "P" ACCOUNT_TYPE_CHOICES = { REGULAR : "Regular" , GOLD : "Gold" , ... F , Q , When >>> # String arguments refer to fields; the following two examples are equivalent: >>> When ( account_type = Client . GOLD , then = "name" ) >>> When ( account_type = Client . GOLD , then = F ( "name" )) >>> # You can use field lookups in ... created using boolean expressions. >>> from django.db.models import Exists , OuterRef >>> non_unique_account_type = ( ... Client . objects . filter ( ... account_type = OuterRef ( "account_type" ), ... ) ... . exclude ( pk = OuterRef ( "pk" )) ... . v ... ... するには、これらすべてのタイプのテストを組み合わせて行うべきです。 概要と簡単な例 テストクライアントを使うには、 django.test.Client からインスタンスを作り、次のようにウェブページを取得します。 >>> from django.test import Client >>> c = Client () >>> resp ... 0 >>> response = c . get ( "/customer/details/" ) >>> response . content b'<!DOCTYPE html...' この例が示唆しているように、 Client のインスタンスは、Python のインタラクティブなインタプリタ上のセッションからでも作ることができます。 テストクライアントの動 ... ことができます。これには、クライアントを作る時に次のように enforce_csrf_checks 引数を渡します。 >>> from django.test import Client >>> csrf_client = Client ( enforce_csrf_checks = True ) リクエストの作成 リクエストの作成には、 django.test.Client クラスを ... ... fix that forms part of the 1.2.2 release, it was necessary to add a feature -- the enforce_csrf_checks flag -- to the test client . This flag forces the test client to perform full CSRF checks on forms. The default behavior of the test client hasn't ch ... anged, but if you want to do CSRF checks with the test client, it is now possible to do so. ... ... ょう。 初めに何かを修正する前に、使用できるツールについて見ておきましょう。 Django テストクライアント Django provides a test Client to simulate a user interacting with the code at the view level. We can use it in tests.py or even in the shell . もう一度 ... 中では、 django.test.TestCase クラス自体がクライアントを持っているため、インポートは不要です)。 >>> from django.test import Client >>> # create an instance of the client for our use >>> client = Client () さて、これでクライアントに仕事を頼む準備ができま ... した。 >>> # get a response from '/' >>> response = client . get ( "/" ) Not Found: / >>> # we should expect a 404 from that address; if you instead see an >>> # "Invalid HTTP_HOST ... ... ール) afirst() (django.db.models.query.QuerySet モジュール) aflush() (backends.base.SessionBase のメソッド) aforce_login() (Client のメソッド) aget() (backends.base.SessionBase のメソッド) (django.db.models.query.QuerySet モジュール) aget_all_permission ... の属性) ALLOWED_HOSTS setting allowlist (EmailValidator の属性) allows_composite_expressions (Expression の属性) alogin() (Client のメソッド) (django.contrib.auth モジュール) alogout() (Client のメソッド) (django.contrib.auth モジュール) alter_db_table ... Model のメソッド) asc() (Expression のメソッド) asend() (Signal のメソッド) asend_robust() (Signal のメソッド) asession() (Client のメソッド) aset() (backends.base.SessionBase のメソッド) (RelatedManager のメソッド) aset_expiry() (backends.base.Session ... ... umentation for TestCase and TransactionTestCase for a full description, and some important notes on database support. Test client improvements A couple of small -- but highly useful -- improvements have been made to the test client: The test Client now ... can automatically follow redirects with the follow argument to Client.get() and Client.post() . This makes testing views that issue redirects simpler. It's now easier to get at the template co ... ntext in the response returned the test client: you'll simply access the context as request.context[key] . The old way, which treats request.context as a list of context ... ... r preventing cross-site request forgery (CSRF) attacks . The CSRF-protection system is based on a random nonce sent to the client in a cookie which must be sent by the client on future requests and, in forms, a hidden value which must be submitted back ... with the form. The caching framework includes an option to cache responses to anonymous (i.e., unauthenticated) clients. When the first anonymous request to a given page is by a client which did not have a CSRF cookie, the cache framework wi ... ll also cache the CSRF cookie and serve the same nonce to other anonymous clients who do not have a CSRF cookie. This can allow an attacker to obtain a valid CSRF cookie value and perform attacks which b ... ... r preventing cross-site request forgery (CSRF) attacks . The CSRF-protection system is based on a random nonce sent to the client in a cookie which must be sent by the client on future requests and, in forms, a hidden value which must be submitted back ... with the form. The caching framework includes an option to cache responses to anonymous (i.e., unauthenticated) clients. When the first anonymous request to a given page is by a client which did not have a CSRF cookie, the cache framework wi ... ll also cache the CSRF cookie and serve the same nonce to other anonymous clients who do not have a CSRF cookie. This can allow an attacker to obtain a valid CSRF cookie value and perform attacks which b ... ... r preventing cross-site request forgery (CSRF) attacks . The CSRF-protection system is based on a random nonce sent to the client in a cookie which must be sent by the client on future requests and, in forms, a hidden value which must be submitted back ... with the form. The caching framework includes an option to cache responses to anonymous (i.e., unauthenticated) clients. When the first anonymous request to a given page is by a client which did not have a CSRF cookie, the cache framework wi ... ll also cache the CSRF cookie and serve the same nonce to other anonymous clients who do not have a CSRF cookie. This can allow an attacker to obtain a valid CSRF cookie value and perform attacks which b ... ... 関数で設定することも、 ConditionalGetMiddleware ミドルウェアを使用して ETag ヘッダを設定してもらうこともできます。 When the client next requests the same resource, it might send along a header such as either If-Modified-Since or If-Unmodified-Since , co ... h or If-None-Match , containing the last ETag it was sent. If the current version of the page matches the ETag sent by the client, or if the resource has not been modified, a 304 status code can be sent back, instead of a full response, telling the cli ... nt that nothing has changed. Depending on the header, if the page has been modified or does not match the ETag sent by the client, a 412 status code (Precondition Failed) may be returned. より細かい粒度のコントロールが必要な場合には、ビューごとの条件付 ... 2026年10月08日