美文网首页
url调度器

url调度器

作者: amarktoo | 来源:发表于2018-05-22 16:03 被阅读0次

    当一个用户请求Django 站点的一个页面,下面是Django 系统决定执行哪个Python 代码使用的算法:

    1.Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has a urlconf attribute (set by middleware), its value will be used in place of the ROOT_URLCONF setting.

    2.Django loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.urls.path() and/or django.urls.re_path() instances.

    3.Django 依次匹配每个URL 模式,在与请求的URL 匹配的第一个模式停下来。

    4.Once one of the URL patterns matches, Django imports and calls the given view, which is a simple Python function (or a class-based view). The view gets passed the following arguments:

       · 一个 HttpRequest 实例。

       · If the matched URL pattern returned no named groups, then the matches from the regular                     expression are provided as positional arguments.

        ·The keyword arguments are made up of any named parts matched by the path expression, overridden by any arguments specified in the optional kwargs argument to django.urls.path() ordjango.urls.re_path().

    5.If no URL pattern matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view. See Error handling below.

    1说django决定了rooturl模块使用,默认是setting中root_urlconf参数指定的module

    但是当有请求有urlconf参数,(在中间件中设置)的情况下将替换项目中默认module,middleware开启在setting中:

    简单自定义middleware:

    看这简单的实现很想python当中的装饰器的概念,但是单独拿出来当一个模块!

    相关文章

      网友评论

          本文标题:url调度器

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