2011-02-18 74 views
2

这个网址的意思是这是我的代码:是什么在Django

(r'^q/(?P<terminal_id>[^/]+)/(?P<cmd_type>[^/]+)/?$', 'send_query_cmd'), 

的观点是:

def send_query_cmd(request, terminal_id, cmd_type): 

啥子约?p意思。

我不知道这是什么意思网址,

感谢

回答

5

(?P<id>REGEXP)是蟒蛇正则表达式命名组捕获的语法。 http://docs.python.org/library/re.html - >>向下滚动至(?P...

至于P表示什么参数?蟒蛇? Origin听起来很有趣。

无论如何,这些相同的正则表达式是django URL解析器用来将URL与视图进行匹配,并将捕获的命名组捕获为视图函数的参数。 http://docs.djangoproject.com/en/dev/topics/http/urls/#captured-parameters

最简单的例子是这样的:

(r'^view/(?P<post_number>\d+)/$', 'foofunc'), 

# we're capturing a very simple regular expression \d+ (any digits) as post_number 
# to be passed on to foofunc 

def foofunc(request, post_number): 
    print post_number 

# visiting /view/3 would print 3. 
+0

我想弄清楚,这是没有任何关系的Django,只是Python的标准正则表达式通过`re`模块处理。 – 2011-02-18 08:30:56

0

它来自Python的regular expression syntax。 (?P ...)语法是一个命名组。这意味着匹配的文本可以使用给定的名称,或者使用Django作为视图函数中的命名参数。如果您只使用括号括起来,那么它就是一个未命名的组,并且可以使用一个整数,该整数是该组被捕获的顺序。

你的URL正则表达式的含义如下......

^ - match the start of the string 
q/ - match a q followed by a slash 
(?P<terminal_id>[^/]+) - match at least one character that isn't a slash, give it the name terminal_id 
/- match a slash 
(?P<cmd_type>[^/]+) - match at least one character that isn't a slash, give it the name cmd_type 
/? - optionality match a slash 
$ - match the end of the string