开发者

what is this url mean in django

this is my code :

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

the view is :

def send_query_cmd(request, terminal_id, cmd开发者_开发技巧_type):

waht about ?p mean .

i dont know what is this url mean ,

thanks


(?P<id>REGEXP) is the syntax for python regular expression named group capturing. http://docs.python.org/library/re.html ->> scroll down to (?P...

As for what the P stands for.. parameter? python? Origin sounds fun.

Anyways, these same regular expressions are what the django URL resolver uses to match a URL to a view, along with capturing named groups as arguments to your view function. http://docs.djangoproject.com/en/dev/topics/http/urls/#captured-parameters

The simplest example is this:

(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. 


It comes from Python regular expression syntax. The (?P...) syntax is a named group. This means that the matched text is available using the given name, or using Django as a named parameter in your view function. If you just use brackets with the ?P then it's an unnamed group and is available using an integer which is the order in which the group was captured.

Your URL regex means the following...

^ - 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜