如何对某些单词有异常 - django url模式中的正则表达式?

问题描述 投票:0回答:1

在我的网站上,人们可以通过像https://example.com/slug-profile这样的网址访问其他人的个人资料,这里是模式:

path('<slug:slug>',views.profile,name='profile')

我也希望用户去settingsnotifications

path('settings',views.param,name='param')
# https://example.com/settings

path('notifications',views.notifications,name='notifications')
# https://example.com/notifications

但在这种情况下,settings将被视为一个slu ..我已经设置了一个功能,以避免用户有像slugsettings这样的notifications

我看到一些网站使用前缀或类似的东西

www.example.com/user/settings/
www.twitter.com/i/notifications/

在我的项目中,我会这样做:

re_path(r'^(?P<slug>\w{5,})$',views.profile,name='profile')

path('i/settings',views.param,name='param')

iuser将永远不会被视为slug配置文件,因为slug正则表达式必须至少包含5个字符。

如何在正则表达式模式中将词语或单词列表['settings','notifications']作为例外?

要么

我需要在网址中使用包含i user的示例吗?

python regex django django-urls
1个回答
2
投票

解决方案比这简单。只需将您的特定网址放在首位;因为模式总是按顺序匹配,所以设置和通知将匹配它们自己的路径而不是段塞路径。

© www.soinside.com 2019 - 2024. All rights reserved.