我有一个 Django Web 应用程序,它有两种类型的用户,比如客户和企业。我需要获取尝试登录的用户类型。所以我定义了一个 url 模式如下:
path('login/<type>/', LoginView.as_view(), name='login'),
但是我怎样才能限制 url 模式只匹配以下模式
- 登录/客户/
- 登录/业务/
我有一个 Django Web 应用程序,它有两种类型的用户,比如客户和企业。我需要获取尝试登录的用户类型。所以我定义了一个 url 模式如下:
path('login/<type>/', LoginView.as_view(), name='login'),
但是我怎样才能限制 url 模式只匹配以下模式
在网址中使用正则表达式。就像是...
url(r'^login/(?P<type>customers|business)', LoginView.as_view(), name='login')
您可以在视图中检查。
if type not in ['customers', 'business']:
messages.error(request, "Invalid Route')
return HttpResponseRedirect('login')
PS:不要使用类型。它已经在 Python 中定义。