re_path()

作者: 花括弧 | 来源:发表于2019-07-24 09:50 被阅读0次

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

from django.urls import include, re_path

函数参数如下:

re_path(route, view, kwargs=None, name=None)
  • The route argument should be a string or gettext_lazy() (see Translating URL patterns) 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.

  • The view argument is a view function or the result of as_view() for class-based views. It can also be an django.urls.include().

  • The kwargs argument allows you to pass additional arguments to the view function or method. See Passing extra options to view functions for an example.

  • See Naming URL patterns for why the name argument is useful.

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:

  • module – URLconf module (or module name)
  • namespace (str) – Instance namespace for the URL entries being included
  • pattern_list – Iterable of path() and/or re_path() instances.
  • app_namespace (str) – Application namespace for the URL entries being included

See Including other URLconfs and URL namespaces and included URLconfs.

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

转载请注明出处

相关文章

  • re_path和path的使用

    1、re_path和path是什么 1、re_path和path的作用都是一样的。只不过re_path是在写url...

  • re_path()

    想要使用该函数,请导入如下模块: 函数参数如下: The route argument should be a s...

  • django3 url路由配置忽略大小写

    django 3 通过re_path来实现正则匹配 路由配置: 浏览器访问

  • 展示HTML网页

    1.导入正则模块:re_path 2.settings配置静态资源 STATICFILES_DIRS = [os....

  • Django 路由与视图

    路由配置 在urls.py中,通过path、re_path、url配置路由其中kwargs入参可传入额外信息,并在...

  • Django报错:AttributeError: 'functi

    在学习Django视图策略的时候,使用基于类的视图 (CBV),遇到了一个问题: re_path(r'^new_p...

  • Django路由系统

    路由系统 获取url上的参数 路径参数(简书) 路径参数的使用 自定义路径参数 re_path 渲染模板 配置模板...

  • Django2.0踩坑笔记

    1. url路径匹配时path方法无法进行正则匹配,需要调用re_path方法才可以 都是亲自实践得出,如有错误请指正

  • djang1.x 和 django2.x不同之处详细避坑

    url和path的区别 Django2.x中的path不支持正则匹配 但在同一目录下的re_path可以 Djan...

  • 路由控制补充--基于Django 2.0+版本

    django2.0的re_path和1.0的url一样 思考情况如下: 考虑下这样的两个问题: 第一个问题,函数 ...

网友评论

      本文标题:re_path()

      本文链接:https://www.haomeiwen.com/subject/krltrctx.html