1. 程式人生 > >django 模組功能

django 模組功能

一.   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])

include(pattern_list)include((pattern_list,app_namespace,instance_namespace))

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 bypatterns()
  • 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 的第一個引數的含義一樣

四.