URLconf で使用するための django.urls 関数

path()

path(route, view, kwargs=None, name=None)

urlpatterns に含める要素を返します。例えば:

from django.urls import include, path

urlpatterns = [
    path("index/", views.index, name="main-view"),
    path("bio/<username>/", views.bio, name="bio"),
    path("articles/<slug:title>/", views.article, name="article-detail"),
    path("articles/<slug:title>/<int:section>/", views.section, name="article-section"),
    path("blog/", include("blog.urls")),
    ...,
]

route

The route argument should be a string or gettext_lazy() (see URL パターンを翻訳する) that contains a URL pattern. The string 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 characters matched and may also change the type of the variable passed to the view. For example, <int:section> matches a string of decimal digits and converts the value to an int.

リクエストを処理する際、Djangoは urlpatterns の最初のパターンから処理を開始し、リストを順番に進みながら、リクエストされたURLと各パターンを比較します。一致するパターンが見つかるまでこの比較を行い、最初に一致したパターンが処理されます。詳細については、Django のリクエスト処理 を参照してください。

URLパターンは、GETやPOSTのパラメータやドメイン名にはマッチしません。例えば、https://www.example.com/myapp/ へのリクエストでは、URLconfは myapp/ を探します。また、https://www.example.com/myapp/?page=3 へのリクエストでも、URLconfは同様に myapp/ を探します。

view

view 引数は、ビュー関数、またはクラスベースビューの場合は as_view() の結果です。また、view 引数には、django.urls.include() を指定することもできます。

Djangoが一致するパターンを見つけると、指定されたビュー関数を呼び出し、最初の引数として HttpRequest オブジェクトを渡し、ルートから「キャプチャされた」値をキーワード引数として渡します。

kwargs

kwargs 引数を使用すると、ビュー関数またはメソッドに追加の引数を渡すことができます。例については、追加的なオプションをビュー関数に渡す を参照してください。

name

URL に名前付けをしておけば Django のどこからでも明確に参照でき、とくにテンプレートの中で有効です。この便利な機能のおかげで、プロジェクトのURLにグローバルな変更を加える場合にも1つのファイルを変更するだけで済むようになります。

name 引数が有用な理由については、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.index, name="index"),
    re_path(r"^bio/(?P<username>\w+)/$", views.bio, name="bio"),
    re_path(r"^blog/", include("blog.urls")),
    ...,
]

The route argument should be a string or gettext_lazy() (see URL パターンを翻訳する) that contains a regular expression compatible with Python's re module. Strings typically use raw string syntax (r'') so that they can contain sequences like \d without the need to escape the backslash with another backslash. When a match is made, captured groups from the regular expression are passed to the view -- as named arguments if the groups are named, and as positional arguments otherwise. The values are passed as strings, without any type conversion.

When a route ends with $ the whole requested URL, matching against path_info, must match the regular expression pattern (re.fullmatch() is used).

The view, kwargs and name arguments are the same as for path().

include()

include(module, namespace=None)[ソース]
include(pattern_list)
include((pattern_list, app_namespace), namespace=None)

この場所に "含める" べき他のURLconfモジュールへのフルPythonインポートパスを取る関数です。オプションで、エントリが含まれる application namespaceinstance namespace も指定できます。

通常、アプリケーションの名前空間は、include されたモジュールによって指定されるべきです。アプリケーションの名前空間が設定されている場合、 namespace 引数を使用して異なるインスタンスの名前空間を設定できます。

include() は、URL パターンを返すイテレータ、またはそのようなイテレータに加えてアプリケーションの名前空間の名前を含む 2値タプルを引数として受け入れることもできます。

パラメータ:
  • module -- URLconf モジュール(またはモジュール名)

  • namespace (str) -- インクルードされる URL エントリのインスタンス名前空間

  • pattern_list -- path() および/または re_path() のインスタンスのイテラブル。

  • app_namespace (str) -- include される URL エントリのアプリケーション名前空間

他の URLconfs をインクルードする および URLの名前空間とインクルードされた URLconfs を参照してください。

register_converter()

register_converter(converter, type_name)[ソース]

The function for registering a converter for use in path() routes.

converter 引数はコンバータークラスであり、 type_name はパスパターンで使用するコンバーター名です。例については 独自のパスコンバータを登録する を参照してください。

URLconf で使用するための django.conf.urls の関数

static()

static.static(prefix, view=django.views.static.serve, **kwargs)

デバッグモードでファイルを配信するための 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)

handler400

handler400

呼び出し可能オブジェクト、またはエラー状態を引き起こし、ステータスコードが400のレスポンスを送信した場合に呼び出されるべきビューへの完全なPythonインポートパスを表す文字列です。 HTTPクライアントがリクエストを送信した場合に該当します。

デフォルトでは、これは django.views.defaults.bad_request() です。カスタムビューを実装する場合は、request および exception 引数を受け入れ、 HttpResponseBadRequest を返すことを確認してください。

handler403

handler403

呼び出し可能オブジェクト、またはユーザーがリソースへのアクセスに必要な権限を持っていない場合に呼び出されるべきビューの完全な Python インポートパスを表す文字列。

デフォルトでは、これは django.views.defaults.permission_denied() です。カスタムビューを実装する場合には、requestexception 引数を受け入れ、 HttpResponseForbidden を返すことを確認してください。

handler404

handler404

呼び出し可能オブジェクト、もしくは、URL パターンに一致するものがない場合に呼び出されるべきビューへの完全な Python インポートパスを表す文字列。

デフォルトでは、これは django.views.defaults.page_not_found() です。カスタムビューを実装する場合は、requestexception 引数を受け取り、HttpResponseNotFound を返すようにしてください。

handler500

handler500

呼び出し可能オブジェクト、もしくはサーバーエラーが発生した場合に呼び出されるべきビューを完全なPythonインポートパスで表した文字列です。サーバーエラーは、ビューコード内で実行時エラーが発生した時に起こります。

デフォルトでは、これは django.views.defaults.server_error() です。独自のビューを実装する場合は、request 引数を受け取り、 HttpResponseServerError を返すことを確認してください。