【发布时间】:2021-03-25 09:52:55
【问题描述】:
我正在尝试创建 2 个在我的应用程序中等效的 url 前缀(这是由于一些审美用户要求),但我不确定解决此问题的最佳方法。最直接的选择是使用备用前缀创建另一组urlpatterns。然而,这将是重复的并且使维护更加困难。有没有更简单的方法让foo/<str:key>/ 等价于bar/<str:key> 和staff/<str:key>/ 等价于foo/<str:key>/staff/?
当前网址
urlpatterns = [
path('foo/<str:key>/welcome/', views.welcome),
path('foo/<str:key>/dashboard/', views.dashboard),
path('foo/<str:key>/staff/dashboard/', views.staff_dashboard),
]
所需的模式
urlpatterns = [
# Original paths
path('foo/<str:key>/welcome/', views.welcome),
path('foo/<str:key>/dashboard/', views.dashboard),
path('foo/<str:key>/staff/dashboard/', views.staff_dashboard),
# Alternate paths to the same pages
path('bar/<str:key>/welcome/', views.welcome),
path('bar/<str:key>/dashboard/', views.dashboard),
path('staff/<str:key>/dashboard/', views.staff_dashboard),
]
【问题讨论】:
标签: python python-3.x django django-urls