Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这个url在django中是什么意思_Python_Regex_Django_Url - Fatal编程技术网

Python 这个url在django中是什么意思

Python 这个url在django中是什么意思,python,regex,django,url,Python,Regex,Django,Url,这是我的代码: (r'^q/(?P<terminal_id>[^/]+)/(?P<cmd_type>[^/]+)/?$', 'send_query_cmd'), 这是什么意思 我不知道这个网址是什么意思 谢谢?PREGEXP是名为group Capture的python正则表达式的语法。 ->>向下滚动到?P 至于p代表什么。。参数python起源听起来很有趣 总之,django URL解析器使用这些正则表达式将URL与视图匹配,并将命名组捕获为视图函数的参数。 最简

这是我的代码:

(r'^q/(?P<terminal_id>[^/]+)/(?P<cmd_type>[^/]+)/?$', 'send_query_cmd'),
这是什么意思

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

谢谢

?PREGEXP是名为group Capture的python正则表达式的语法。 ->>向下滚动到?P

至于p代表什么。。参数python起源听起来很有趣

总之,django URL解析器使用这些正则表达式将URL与视图匹配,并将命名组捕获为视图函数的参数。

最简单的例子是:

(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. 
它来自Python。那个?P。。。语法是一个命名组。这意味着匹配的文本可以使用给定的名称,或者使用Django作为视图函数中的命名参数。如果您只是将括号与?P一起使用,那么它是一个未命名的组,并且可以使用整数获取,整数是捕获组的顺序

你的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

澄清一下,这与Django无关,只是Python通过re模块进行的标准正则表达式处理。
^ - 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