【问题标题】:what is this url mean in django这个url在django中是什么意思
【发布时间】:2011-06-29 13:02:01
【问题描述】:

这是我的代码:

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

观点是:

def send_query_cmd(request, terminal_id, cmd_type):

关于?p 的意思。

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

谢谢

【问题讨论】:

    标签: python regex django url


    【解决方案1】:

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

    至于P代表什么..参数? Python?起源听起来很有趣。

    无论如何,这些相同的正则表达式是 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. 
    

    【讨论】:

    • 澄清一下,这与 Django 无关,只是 Python 通过 re 模块处理的标准正则表达式。
    【解决方案2】:

    它来自 Python regular expression syntax。 (?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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2018-10-29
      相关资源
      最近更新 更多