re_path()

2019-07-24  本文已影响0人  花括弧

想要使用该函数,请导入如下模块:

from django.urls import include, re_path

函数参数如下:

re_path(route, view, kwargs=None, name=None)
Naming URL patterns(name argument in re_path)

In order to perform URL reversing, you’ll need to use named URL patterns. The string used for the URL name can contain any characters you like. You are not restricted to valid Python names.

When naming URL patterns, choose names that are unlikely to clash with other applications’ choice of names. If you call your URL pattern comment and another application does the same thing, the URL that reverse() finds depends on whichever pattern is last in your project’s urlpatterns list.

Putting a prefix on your URL names, perhaps derived from the application name (such as myapp-comment instead of comment), decreases the chance of collision.

You can deliberately choose the same URL name as another application if you want to override a view. For example, a common use case is to override the LoginView. Parts of Django and most third-party apps assume that this view has a URL pattern with the name login. If you have a custom login view and give its URL the name login, reverse() will find your custom view as long as it’s in urlpatterns after django.contrib.auth.urls is included (if that’s included at all).

You may also use the same name for multiple URL patterns if they differ in their arguments. In addition to the URL name, reverse() matches the number of arguments and the names of the keyword arguments.(in addition to: 除了...)

include()

include(module, namespace=None)[source]

include(pattern_list)

include((pattern_list, app_namespace), namespace=None)

include function that takes a full Python import path to another URLconf module that should be “included” in this place.

Optionally, the application namespace and instance namespace where the entries will be included into can also be specified.

Usually, the application namespace should be specified by the included module. If an application namespace is set, the namespace argument can be used to set a different instance namespace.

include() also accepts as an argument either an iterable that returns URL patterns or a 2-tuple containing such iterable plus the names of the application namespaces.

Parameters:

See Including other URLconfs and URL namespaces and included URLconfs.

初学于此,鉴于 url匹配规则有点多,就先到这里吧。

转载请注明出处

上一篇 下一篇

猜你喜欢

热点阅读