... >> When ( Q ( then = 0 ), then = 1 ) Case class Case ( * cases , ** extra ) [ソース] Case() 式は Python の if ... elif ... else 文のようなものです。指定された When() オブジェクトの各 condition は 1 つが True と評価されるまで順番に評価されます。一致し ... 件がJane DoeとJack Blackの両方にマッチしても正しい結果が得られることを覚えておいてください。これは Python の if ... elif ... else 文と同じように動作します。 case() は filter() 句でも使えます。たとえば、1ヶ月以上前に登録されたゴールドクライアントと1年以 ... データベースでは、これは CASE ステートメントを使ってエミュレートされます: SELECT count ( CASE WHEN account_type = 1 THEN id ELSE null ) as regular , count ( CASE WHEN account_type = 2 THEN id ELSE null ) as gold , count ( CASE WHEN account_type = 3 TH ... ... ます。たとえば: from django.conf import settings def my_view ( request ): if settings . SITE_ID == 3 : # Do something. pass else : # Do something else. pass It's fragile to hardcode the site IDs like that, in case they change. The cleaner way of accom ... _view ( request ): current_site = get_current_site ( request ) if current_site . domain == "foo.com" : # Do something pass else : # Do something else. pass これには、サイトフレームワークがインストールされているかどうかを確認するという利点もあります。 ... out_request (): current_site = Site . objects . get_current () if current_site . domain == "foo.com" : # Do something pass else : # Do something else. pass 表示用の現在のドメインを取得する LJWorld.com と Lawrence.com はどちらも、ニュースが発生したとき ... ... if form . is_valid (): handle_uploaded_file ( request . FILES [ "file" ]) return HttpResponseRedirect ( "/success/url/" ) else : form = UploadFileForm () return render ( request , "upload.html" , { "form" : form }) ここで、 request.FILES をフォームの ... , request . FILES ) if form . is_valid (): # file is saved form . save () return HttpResponseRedirect ( "/success/url/" ) else : form = ModelFormWithFileField () return render ( request , "upload.html" , { "form" : form }) 手動でオブジェクトを作成す ... ithFileField ( file_field = request . FILES [ "file" ]) instance . save () return HttpResponseRedirect ( "/success/url/" ) else : form = UploadFileForm () return render ( request , "upload.html" , { "form" : form }) リクエストの外で手動でオブジェクト ... ... thenticate ( username = "john" , password = "secret" ) if user is not None : # A backend authenticated the credentials ... else : # No backend authenticated the credentials ... request はオプションで、 HttpRequest のインスタンスを取ります。このインス ... icated を用いて次のように識別できます。 if request . user . is_authenticated : # Do something for authenticated users. ... else : # Do something for anonymous users. ... 非同期ビューでは次のようになります。 user = await request . auser () if user . i ... s_authenticated : # Do something for authenticated users. ... else : # Do something for anonymous users. ... ユーザーをログインさせる方法 現在のセッションに接続したい認証ユーザーがいる場合、 ... ... ush the branch to your fork and open a pull request. See Submitting contributions for the full process. Continuing someone else's work If you're continuing the work from someone else's pull request on GitHub, things can be tricky. You need to pull th ... ... he process for suggesting new features . If a ticket for this issue already exists and has been accepted, make sure nobody else has claimed it. To do this, look at the "Owned by" section of the ticket. If it's assigned to "nobody," then it's availabl ... e to be claimed. Otherwise, somebody else may be working on this ticket. Either find another bug/feature to work on, or contact the developer working on the ticket ... week or two, another developer may ask you to relinquish the ticket claim so that it's no longer monopolized and somebody else can claim it. If you've claimed a ticket and it's taking a long time (days or weeks) to code, keep everybody updated by po ... ... あります。: < ul > {% if athlete_list %} {% for athlete in athlete_list %} < li > {{ athlete.name }} </ li > {% endfor %} {% else %} < li > Sorry, no athletes in this list. </ li > {% endif %} </ ul > if {% if %} タグは変数を評価し、その変数が "true" で ... tes: {{ athlete_list | length }} {% elif athlete_in_locker_room_list %} Athletes should be out of the locker room soon! {% else %} No athletes. {% endif %} 上の例では、 athlete_list が空でなければ、アスリートの人数を {{ athlete_list|length }} 変数で表 ... 示します。 例にもあるように if タグはオプションで 1 個以上の {% elif %} 節、および 1 個の {% else %} 節をつけることができます。 {% else %} はそれまでの評価結果がすべて True でなかった場合に表示されるコンテンツを定義します ... ... athlete_list : < ul > {% for athlete in athlete_list %} < li > {{ athlete.name }} </ li > {% endfor %} </ ul > if , elif , else 変数を評価し、その変数が "true "の場合、ブロックの内容が表示されます: {% if athlete_list %} Number of athletes: {{ athlete ... _list | length }} {% elif athlete_in_locker_room_list %} Athletes should be out of the locker room soon! {% else %} No athletes. {% endif %} 上記の場合、 athlete_list が空でなければ、 {{ athlete_list|length }} 変数によって選手の数が表示 ... な演算子を使うこともできます: {% if athlete_list | length > 1 %} Team: {% for athlete in athlete_list %} ... {% endfor %} {% else %} Athlete: {{ athlete_list.0.name }} {% endif %} 上の例はうまくいきますが、ほとんどのテンプレートフィルタは文字列を返すの ... ... ( self , form ): response = super () . form_invalid ( form ) if self . request . accepts ( "text/html" ): return response else : return JsonResponse ( form . errors , status = 400 ) def form_valid ( self , form ): # We make sure to call the parent's ... ave() for example). response = super () . form_valid ( form ) if self . request . accepts ( "text/html" ): return response else : data = { "pk" : self . object . pk , } return JsonResponse ( data ) class AuthorCreateView ( JsonableResponseMixin , Cre ... ... direct to a new URL: return HttpResponseRedirect ( "/thanks/" ) # if a GET (or any other method) we'll create a blank form else : form = NameForm () return render ( request , "name.html" , { "form" : form }) もし GET とともにビューにたどり着いた場合は ... d through the contact form. \n " f "Contact email (unverified): { contact_email } \n " f "Marked urgent: { 'yes' if urgent else 'no' } \n " f "Message: \n { message } " ) # Send the message, directing replies to the contact_email. EmailMessage ( subj ... ody , from_email = "contact-form@example.com" , to = [ "info@example.com" ], headers = { "Importance" : "high" } if urgent else None , reply_to = [ contact_email ], ) . send () return HttpResponseRedirect ( "/thanks/" ) Tip Django からメールを送信する ... 2026年10月08日