0

我有一个 Django Web 应用程序,它有两种类型的用户,比如客户和企业。我需要获取尝试登录的用户类型。所以我定义了一个 url 模式如下:

 path('login/<type>/', LoginView.as_view(), name='login'),

但是我怎样才能限制 url 模式只匹配以下模式

  1. 登录/客户/
  2. 登录/业务/
4

2 回答 2

3

在网址中使用正则表达式。就像是...


url(r'^login/(?P<type>customers|business)', LoginView.as_view(), name='login')
于 2020-11-30T04:02:14.310 回答
0

您可以在视图中检查。

if type not in ['customers', 'business']:
    messages.error(request, "Invalid Route')
    return HttpResponseRedirect('login')

PS:不要使用类型。它已经在 Python 中定义。

于 2020-11-30T03:18:28.890 回答