... 場合 ( middleware で設定されます) 、その値を ROOT_URLCONF の代わりに使います。 Django はその Python モジュールをロードして、 urlpatterns という名前の変数を探します。この変数の値は django.urls.path() または django.urls.re_path() インスタンスの sequence でなけれ ... の Error handling を見てください。 カスタマイズ例 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 ( ... , register_converter from . import converters , views register_converter ( converters . FourDigitYearConverter , "yyyy" ) urlpatterns = [ path ( "articles/2003/" , views . special_case_2003 ), path ( "articles/<yyyy:year>/" , views . year_archive ), ... , ... ... mple, <int:section> matches a string of decimal digits and converts the value to an int . リクエストを処理する際、Djangoは urlpatterns の最初のパターンから処理を開始し、リストを順番に進みながら、リクエストされたURLと各パターンを比較します。一致するパターンが ... な理由については、 URL パターンの命名 を参照してください。 re_path() re_path ( route , view , kwargs = None , name = None ) urlpatterns に含める要素を返します。例えば: from django.urls import include , re_path urlpatterns = [ re_path ( r "^index/$" , views . ... 信するための URL パターンを返すヘルパー関数です: from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static ( settings . MEDIA_URL , document_root = settings . MEDIA_ROOT ... ... py : from django.urls import path from django.views.generic.dates import ArchiveIndexView from myapp.models import Article urlpatterns = [ path ( "archive/" , ArchiveIndexView . as_view ( model = Article , date_field = "pub_date" ), name = "article_archive" ... = True allow_future = True 例 myapp/urls.py : from django.urls import path from myapp.views import ArticleYearArchiveView urlpatterns = [ path ( "<int:year>/" , ArticleYearArchiveView . as_view (), name = "article_year_archive" ), ] 例 myapp/article_archiv ... _date" allow_future = True 例 myapp/urls.py : from django.urls import path from myapp.views import ArticleMonthArchiveView urlpatterns = [ # Example: /2012/08/ path ( "<int:year>/<int:month>/" , ArticleMonthArchiveView . as_view ( month_format = "%m" ), nam ... ... latpages' を追加してください。 そして、下記のいずれかを行います: URLconf にエントリを追加します。例えば、次のようにします: urlpatterns = [ path ( "pages/" , include ( "django.contrib.flatpages.urls" )), ] または: django.contrib.flatpages.middleware.Flatpage ... フラットページをURLconfに含める方法はいくつかあります。以下のように、特定のパスをフラットページに割り当てることができます。 urlpatterns = [ path ( "pages/" , include ( "django.contrib.flatpages.urls" )), ] 「キャッチオール」パターンとして設定することもできま ... す。この場合、そのパターンを他の urlpatterns の最後に配置することが重要です。 from django.contrib.flatpages import views # Your other patterns here urlpatterns += [ re ... ... port path from blog.models import Entry info_dict = { "queryset" : Entry . objects . all (), "date_field" : "pub_date" , } urlpatterns = [ # some generic view using info_dict # ... # the sitemap path ( "sitemap.xml" , sitemap , { "sitemaps" : { "blog" : Gen ... urls import path from .sitemaps import StaticViewSitemap from . import views sitemaps = { "static" : StaticViewSitemap , } urlpatterns = [ path ( "" , views . main , name = "main" ), path ( "about/" , views . about , name = "about" ), path ( "license/" , vi ... 数を受け取るべきです。 上記の例に対する関連するURLconfの行は、次のようになります: from django.contrib.sitemaps import views urlpatterns = [ path ( "sitemap.xml" , views . index , { "sitemaps" : sitemaps }, name = "django.contrib.sitemaps.views.index" , ), pa ... ... owed). This allows a much more natural use of URLconfs. For example, this URLconf: from django.conf.urls.defaults import * urlpatterns = patterns ( "" , ( "^myview/$" , "mysite.myapp.views.myview" )) can now be rewritten as: from django.conf.urls.defaults i ... mport * from mysite.myapp.views import myview urlpatterns = patterns ( "" , ( "^myview/$" , myview )) One useful application of this can be seen when using decorators; this change ... list_detail import object_list from mysite.myapp.models import MyModel info = { "queryset" : MyModel . objects . all (), } urlpatterns = patterns ( "" , ( "^myview/$" , login_required ( object_list ), info )) Note that both syntaxes (strings and callables) ... ... s_view() のメソッド呼び出しに渡せばよいのです。 from django.urls import path from django.views.generic import TemplateView urlpatterns = [ path ( "about/" , TemplateView . as_view ( template_name = "about.html" )), ] as_view() に渡されたすべての引数は、クラ ... スビューに対する関数ライクな要素を指定できます。 # urls.py from django.urls import path from some_app.views import AboutView urlpatterns = [ path ( "about/" , AboutView . as_view ()), ] 組み込みの汎用ビューの使用方法の詳細については、次のトピック 汎用のクラス ... 、本のリストビューへの URL を次のようにマッピングします。 from django.urls import path from books.views import BookListView urlpatterns = [ path ( "books/" , BookListView . as_view ()), ] そして、ビューには次のように書きます。 from django.http import HttpRes ... ... go.conf import settings from django.contrib.staticfiles import views from django.urls import re_path if settings . DEBUG : urlpatterns += [ re_path ( r "^static/(?P<path>.*)$" , views . serve ), ] 注意: パターンの先頭 ( r'^static/' ) はあなたの設定 ( STATIC ... _URL ) であるべきです。 これは少し厄介なので、これを代行してくれるヘルパー関数もあります: urls. staticfiles_urlpatterns () これは、すでに定義されているパターンリストに静的ファイルを提供するための適切なURLパターンを返します。次のように使います ... : from django.contrib.staticfiles.urls import staticfiles_urlpatterns # ... the rest of your URLconf here ... urlpatterns += staticfiles_urlpatterns () これにより、 STATIC_URL 設定が検査され、 ... ... uture %} tags. django.conf.urls.patterns() In the olden days of Django, it was encouraged to reference views as strings in urlpatterns : urlpatterns = patterns ( "" , url ( "^$" , "myapp.views.myview" ), ) and Django would magically import myapp.views.myvie ... the patterns() function takes a required initial prefix argument which is prepended to all views-as-strings in that set of urlpatterns : urlpatterns = patterns ( "myapp.views" , url ( "^$" , "myview" ), url ( "^other/$" , "otherview" ), ) In the modern era, ... ays, the above use of the prefix arg is much more likely to be written (and is better written) as: from myapp import views urlpatterns = patterns ( "" , url ( "^$" , views . myview ), url ( "^other/$" , views . otherview ), ) Thus patterns() serves little p ... ... ALLED_APPS are provided in the JavaScript output. デフォルト値を使った例 : from django.views.i18n import JavaScriptCatalog urlpatterns = [ path ( "jsi18n/" , JavaScriptCatalog . as_view (), name = "javascript-catalog" ), ] 独自のパッケージを使った例 : urlpa ... 18n_patterns() によるラップも必要となります。 i18n_patterns() を使った例 : from django.conf.urls.i18n import i18n_patterns urlpatterns = i18n_patterns ( path ( "jsi18n/" , JavaScriptCatalog . as_view (), name = "javascript-catalog" ), ) 翻訳の優先順位は、 p ... rom django.views.i18n import JavaScriptCatalog # The value returned by get_version() must change when translations change. urlpatterns = [ path ( "jsi18n/" , cache_page ( 86400 , key_prefix = "jsi18n- %s " % get_version ())( JavaScriptCatalog . as_view () ) ... 2026年10月08日