简体   繁体   中英

Wild card pattern in Python url

I am new in Django and need url pattern to match everything (particularly uuid). The question and answer may seem very simple, but I need your help here. The below is my code:

# parent urls.py
urlpatterns = [
    path('admin/', admin.site.urls),
    path('alerting/', include('alerting.urls')),
]

# alerting/urls.py
urlpatterns = [
    path('', views.index, name='index'),
    path('test', views.test, name='test'),
    path('.*', views.test, name='uuid'),
]

I've tried in many ways but could find how make it work.

Just add a parameter to capture:

path('<uuid>/', views.test, name='uuid'),

And retrieve this parameter in your view:

def test(request, uuid):
    ...

请注意,如果您需要专门匹配uuid而不是仅匹配所有内容,则可以使用一个路径转换器

path('<uuid:uuid>/', views.test, name='uuid'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM