一. django.utils.six
非常有用的判断处理版本的模块,用于使程序同时兼容python的2版本和3版本。
例如:
django.utils.six.string_types 在 py3 里是类型 str,在py2里是类型 six.string_types。在类型判断时非常有用,提高程序的兼容性。
二. django.core.urlresolvers
This module converts requested URLs to callback view functions.
URLs 回调 view 函数
例如:
django.core.urlresolvers.RegexURLPattern 的构造函数会判断参数callback是否为一个字符串(模块路径或view里的函数名)或是一个view中可调用的对象。
三. django.conf.urls
有三个接口
include(module[, namespace=None, app_name=None])
Parameters:
- module – URLconf module (or module name)
- namespace (string) – Instance namespace for the URL entries being included
- app_name (string) – Application namespace for the URL entries being included
- pattern_list – Iterable of URL entries as returned by patterns()
- app_namespace (string) – Application namespace for the URL entries being included
- instance_namespace (string) – Instance namespace for the URL entries being included
2. patterns
patterns(prefix, pattern_description, ...)
参数为一个正则和任意数量的URL patterns,返回URL patterns的列表。
URL patterns 型式应该为
(regular expression, Python callback function [, optional_dictionary [, optional_name]])
patterns 函数只能最大有255个参数,这一般来说不是什么问题。
3.url
url(regex, view, kwargs=None, name=None, prefix='')
url 函数用来代替元组作为 patterns 的参数,有以下好处。
(1) 指定一个参数 name
urlpatterns = patterns('',
url(r'^index/$', index_view, name="main-view"),
...
)
在使用
reverse() 函数或使用
url标签时尤其有用
(2) kwargs 参数可以给视图函数传递额外的变量。这种技术主要被用于 The syndication feed framework(RSS andAtom).
(3) prefix 参数和函数 pattern 的第一个参数的含义一样
四.