【问题标题】:How to convert URL() to path() in Django?如何在 Django 中将 URL() 转换为 path()?
【发布时间】:2022-09-29 19:32:17
【问题描述】:
url_patterns=[
        url(r\'^(?P<postid>\\d+)/preference/(?P<userpreference>\\d+)/$\', views.postpreference, name=\'postpreference\'),
]

我仅从 Stack Overflow 获得此代码,但我只知道 path() 但不知道 url()

我试图理解并使用url() to path() 将这个url(r\'^......) 转换为path(...)

但我也想知道如何在path() 中使用\"\\d+\"

    标签: python django django-urls


    【解决方案1】:

    \d+一般用于整数,所以使用the &lt;int:&gt; converter

    path('<int:postid>/preference/<int:userpreference>/', views.postpreference, name='postpreference'),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 2016-07-26
      • 1970-01-01
      • 2018-10-08
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多