【问题标题】:Django - urls.py REGEXDjango - urls.py 正则表达式
【发布时间】:2013-07-12 04:43:28
【问题描述】:

我在为 Django 项目编写 url 的正则表达式部分时遇到了一些麻烦。

我希望能够在捕获配置文件 ID 的同时进行路由

网址示例:www.somesite.com/profile/12345678901

这是我目前所拥有的

url(r'^profile/$', views.dashboard, name='widget')

应该在 profile/ 之后的正确正则表达式是什么? 我将如何捕获 profile/ 之后的数字部分?一旦我在视图中?

谢谢,

【问题讨论】:

    标签: django django-urls


    【解决方案1】:
    url(r'^profile/(?P<id>[\d]+)/$', profile_view, name="profile"),
    

    在视图中

    def profile_view(request, id):
        #some logic here...
    

    【讨论】:

    • +1。虽然这可行,但我会推荐 Named groups - 类似 url(r'^profile/(?P&lt;id&gt;[\d]+)/$', profile_view, name="profile"), 的东西,让事情更明确
    猜你喜欢
    • 2014-06-14
    • 2012-07-22
    • 1970-01-01
    • 2013-03-23
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    相关资源
    最近更新 更多