Restricting the url parameter values in Django URL

464 Views Asked by At

I have a Django web app that has 2 types of users, say customers, and business. I need to get the type of user trying to login. So I defined a url pattern as folows:

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

But how can I restrict the url patten to match only the following patterns

  1. login/customers/
  2. login/business/
2

There are 2 best solutions below

2
On BEST ANSWER

use regex in the url. Something like...


url(r'^login/(?P<type>customers|business)', LoginView.as_view(), name='login')
2
On

You can check that in views.

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

PS: Do not use type. Its already defined in Python.