侮辱尤娜,从化汽车站到佛冈,白头鹀
目录
django的url dispatcher 设计是基于一个url mapper来工作的。
这个url mapper主要用在两个方向:
通过提供的标识,反解出url
django provides a solution such that the url mapper is the only repository of the url design. you feed it with your urlconf and then it can be used in both directions:
** starting with a url requested by the user/browser, it calls the right django view providing any arguments it might need with their values as extracted from the url.
** starting with the identification of the corresponding django view plus the values of arguments that would be passed to it, obtain the associated url.
django的url 模式非常的清晰和优雅。一个高质量的web应用就需要一个好的url模式。
django的url 助记点:
通过对比两个类的定义:
看到,urlresolver也有resovle解析方法。只不过urlresolver的解析会再去加载子url module模块中的urlpatterns列表。然后再对列表中的进行循环匹配过程,一直嵌套下去,知道最后的return跳出返回一个resolvermatch对象。而urlpattern的resolver直接就返回resovlermatch对象了。只不过前者会有重新加载获取子url module模块来获取urlpatterns的逻辑。
两个类都用同名的方法,只是表现出来的的状态有所不同。这就是面向对象多态在代码中的体现。提供相同的对外接口,展现出来的状态过程有所不同,最后返回相同的对象。
子路由除了减少路由前缀的冗余,还可以满足多种url前缀使用同一app的业务场景。
参照源码,从最low-level源码层面的方式,参照实例化出urlresolver对象的源码
if isinstance(view, (list, tuple)): # 这里的view是re_path或path的第二个参数 # for include(...) processing. pattern = pattern(route, is_endpoint=false) urlconf_module, app_name, namespace = view return urlresolver( pattern, urlconf_module, kwargs, app_name=app_name, namespace=namespace, )
从源码可以看出,如果view参数是一个列表或元组类型,那么将会实例化出urlresolver对象,并且对view参数要有且有三个元素。第一个元素可以是子路由的模块的python path 也可以直接是 url对象的列表(查看urlresolver.url_patterns源码可以理解);第二个元素和第三个元素都可以空,也可以都有,但是不能只有namespace单独有。
django内置的from django.urls import include 提供生成第一种方式view参数的函数
include源码:
def include(arg, namespace=none): app_name = none if isinstance(arg, tuple): # callable returning a namespace hint. try: urlconf_module, app_name = arg except valueerror: if namespace: raise improperlyconfigured( 'cannot override the namespace for a dynamic module that ' 'provides a namespace.' ) raise improperlyconfigured( 'passing a %d-tuple to include() is not supported. pass a ' '2-tuple containing the list of patterns and app_name, and ' 'provide the namespace argument to include() instead.' % len(arg) ) else: # no namespace hint - use manually provided namespace. urlconf_module = arg if isinstance(urlconf_module, str): urlconf_module = import_module(urlconf_module) patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module) app_name = getattr(urlconf_module, 'app_name', app_name) if namespace and not app_name: raise improperlyconfigured( 'specifying a namespace in include() without providing an app_name ' 'is not supported. set the app_name attribute in the included ' 'module, or pass a 2-tuple containing the list of patterns and ' 'app_name instead.', ) namespace = namespace or app_name # make sure the patterns can be iterated through (without this, some # testcases will break). if isinstance(patterns, (list, tuple)): for url_pattern in patterns: pattern = getattr(url_pattern, 'pattern', none) if isinstance(pattern, localeprefixpattern): raise improperlyconfigured( 'using i18n_patterns in an included urlconf is not allowed.' ) return (urlconf_module, app_name, namespace)
可以看到提供app_name 而不提供namespace的话是会抛出异常的。
notice:关于app_name 与 namespace 存在这样一个依赖逻辑:
inlucde()的参数方式,也有几种:
和from django.urls import reverse 函数的源码。大致可以这样理解:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论