1 def url(regex, view, kwargs=None, name=None, prefix=‘‘): 2 if isinstance(view, (list,tuple)): 3 # For include(...) processing. 4 urlconf_module, app_name, namespace = view 5 return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace) 6 else: 7 if isinstance(view, six.string_types): 8 if not view: 9 raise ImproperlyConfigured(‘Empty URL pattern view name not permitted (for pattern %r)‘ % regex) 10 if prefix: 11 view = prefix + ‘.‘ + view 12 return RegexURLPattern(regex, view, kwargs, name)
from django import urls
2:第二行的 isinstance(view, (list,tuple))判断类型,如果是列表或者元组,
4:urlconf_module, app_name, namespace = view 把view的值赋给这三个变量
5:return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace) 待查看
7:isinstance(view, six.string_types):假如view是
basestring
basestring是str和unicode的超类(父类),也是抽象类,因此不能被调用和实例化,但可以被用来判断一个对象是否为str或者unicode的实例,isinstance(obj, basestring)等价于isinstance(obj, (str, unicode));
8:如果没有views,raise ImproperlyConfigured(‘Empty URL pattern view name not permitted (for pattern %r)‘ % regex)
raise
抛出异常,
10:如果prefix(前缀)有前缀,就拼接在一起,然后调用 REGEXURLPattern
url函数的参数:
regex是正则表达式
view
kwargs
name 就是那个 name=‘blog‘
prefix
时间: 2024-10-26 23:51:35