... ださい。 カスタマイズ例 ¶ URLconf のサンプルです。 from django.urls import path from . import views urlpatterns = [ path ( "articles/2003/" , views . special_case_2003 ), path ( "articles/<int:year>/" , views . year_archive ), path ( "articles/<int:year>/ ... <int:month>/" , views . month_archive ), path ( "articles/<int:year>/<int:month>/<slug:slug>/" , views . article_detail ), ] メモ: URLから値をキャプチャするには、角括弧を使用します ... ンバータが含まれていない場合は、 / 文字を除いた文字列がマッチします。 スラッシュを先頭につける必要はありません。たとえば、 /articles ではなく articles です。 リクエストの例: articles/2005/03/ へのリクエストはリストの 3 番目のエントリにマッチします。Django ... ... ださい。 カスタマイズ例 ¶ URLconf のサンプルです。 from django.urls import path from . import views urlpatterns = [ path ( "articles/2003/" , views . special_case_2003 ), path ( "articles/<int:year>/" , views . year_archive ), path ( "articles/<int:year>/ ... <int:month>/" , views . month_archive ), path ( "articles/<int:year>/<int:month>/<slug:slug>/" , views . article_detail ), ] メモ: URLから値をキャプチャするには、角括弧を使用します ... ンバータが含まれていない場合は、 / 文字を除いた文字列がマッチします。 スラッシュを先頭につける必要はありません。たとえば、 /articles ではなく articles です。 リクエストの例: articles/2005/03/ へのリクエストはリストの 3 番目のエントリにマッチします。Django ... ... o_response() 例 myapp/views.py : from django.utils import timezone from django.views.generic.detail import DetailView from articles.models import Article class ArticleDetailView ( DetailView ): model = Article def get_context_data ( self , ** kwargs ): c ... der_to_response() Example views.py : from django.utils import timezone from django.views.generic.list import ListView from articles.models import Article class ArticleListView ( ListView ): model = Article paginate_by = 100 # if pagination is desired def ... w urlpatterns = [ path ( "" , ArticleListView . as_view (), name = "article-list" ), ] 例 myapp/article_list.html : < h1 > Articles </ h1 > < ul > {% for article in object_list %} < li > {{ article.pub_date | date }} - {{ article.headline }} </ li > {% e ... ... The API follows relationships as far as you need, performing efficient # JOINs for you behind the scenes. # This finds all articles by a reporter whose name starts with "John". >>> Article . objects . filter ( reporter__full_name__startswith = "John" ) < ... は、 URLconf は以下のようになります: news/urls.py ¶ from django.urls import path from . import views urlpatterns = [ path ( "articles/<int:year>/" , views . year_archive ), path ( "articles/<int:year>/<int:month>/" , views . month_archive ), path ( "articl ... す。 各ビューは、リクエストのメタデータを含むリクエストオブジェクトと、パターンで捉えられた値を渡します。 例えば、ユーザが "/articles/2005/05/39323/" という URL をリクエストすると、 Django は news.views.article_detail(request, year=2005, month=5, pk=39323) ... ... The API follows relationships as far as you need, performing efficient # JOINs for you behind the scenes. # This finds all articles by a reporter whose name starts with "John". >>> Article . objects . filter ( reporter__full_name__startswith = "John" ) < ... は、 URLconf は以下のようになります: news/urls.py ¶ from django.urls import path from . import views urlpatterns = [ path ( "articles/<int:year>/" , views . year_archive ), path ( "articles/<int:year>/<int:month>/" , views . month_archive ), path ( "articl ... す。 各ビューは、リクエストのメタデータを含むリクエストオブジェクトと、パターンで捉えられた値を渡します。 例えば、ユーザが "/articles/2005/05/39323/" という URL をリクエストすると、 Django は news.views.article_detail(request, year=2005, month=5, pk=39323) ... ... o_response() 例 myapp/views.py : from django.utils import timezone from django.views.generic.detail import DetailView from articles.models import Article class ArticleDetailView ( DetailView ): model = Article def get_context_data ( self , ** kwargs ): c ... der_to_response() Example views.py : from django.utils import timezone from django.views.generic.list import ListView from articles.models import Article class ArticleListView ( ListView ): model = Article paginate_by = 100 # if pagination is desired def ... w urlpatterns = [ path ( "" , ArticleListView . as_view (), name = "article-list" ), ] 例 myapp/article_list.html : < h1 > Articles </ h1 > < ul > {% for article in object_list %} < li > {{ article.pub_date | date }} - {{ article.headline }} </ li > {% e ... ... p.forms import ArticleForm >>> class BaseArticleFormSet ( BaseFormSet ): ... def clean ( self ): ... """Checks that no two articles have the same title.""" ... if any ( self . errors ): ... # Don't bother validating the formset unless each form is valid ... rm ): ... continue ... title = form . cleaned_data . get ( "title" ) ... if title in titles : ... raise ValidationError ( "Articles in a set must have distinct titles." ) ... titles . add ( title ) ... >>> ArticleFormSet = formset_factory ( ArticleForm , ... = ArticleFormSet ( data ) >>> formset . is_valid () False >>> formset . errors [{}, {}] >>> formset . non_form_errors () ['Articles in a set must have distinct titles.'] フォームセットの clean メソッドは、 Form.clean メソッドが呼ばれた後に呼び出されます。 ... ... p.forms import ArticleForm >>> class BaseArticleFormSet ( BaseFormSet ): ... def clean ( self ): ... """Checks that no two articles have the same title.""" ... if any ( self . errors ): ... # Don't bother validating the formset unless each form is valid ... rm ): ... continue ... title = form . cleaned_data . get ( "title" ) ... if title in titles : ... raise ValidationError ( "Articles in a set must have distinct titles." ) ... titles . add ( title ) ... >>> ArticleFormSet = formset_factory ( ArticleForm , ... = ArticleFormSet ( data ) >>> formset . is_valid () False >>> formset . errors [{}, {}] >>> formset . non_form_errors () ['Articles in a set must have distinct titles.'] フォームセットの clean メソッドは、 Form.clean メソッドが呼ばれた後に呼び出されます。 ... ... () http_method_not_allowed() get_context_data() Example views.py : from django.views.generic.base import TemplateView from articles.models import Article class HomePageView ( TemplateView ): template_name = "home.html" def get_context_data ( self , ** kw ... args ): context = super () . get_context_data ( ** kwargs ) context [ "latest_articles" ] = Article . objects . all ()[: 5 ] return context Example urls.py : from django.urls import path from myapp.views impor ... Example views.py : from django.shortcuts import get_object_or_404 from django.views.generic.base import RedirectView from articles.models import Article class ArticleCounterRedirectView ( RedirectView ): permanent = False query_string = True pattern_nam ... ... () http_method_not_allowed() get_context_data() Example views.py : from django.views.generic.base import TemplateView from articles.models import Article class HomePageView ( TemplateView ): template_name = "home.html" def get_context_data ( self , ** kw ... args ): context = super () . get_context_data ( ** kwargs ) context [ "latest_articles" ] = Article . objects . all ()[: 5 ] return context Example urls.py : from django.urls import path from myapp.views impor ... Example views.py : from django.shortcuts import get_object_or_404 from django.views.generic.base import RedirectView from articles.models import Article class ArticleCounterRedirectView ( RedirectView ): permanent = False query_string = True pattern_nam ... 2026年10月08日