0

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/
Abhijith Konnayil
  • 4,067
  • 6
  • 23
  • 49

2 Answers2

3

use regex in the url. Something like...


url(r'^login/(?P<type>customers|business)', LoginView.as_view(), name='login')
ha-neul
  • 3,058
  • 9
  • 24
  • if i proceed with the first option ..how can i get the parameter whether it is customer or business? and how can do the 2 nd method, that is what I really need – Abhijith Konnayil Nov 30 '20 at 04:04
  • Hi thanks for accepting my answer. I deleted the first one as it may not work for your case. that type of solution works well for when you have opitonal_varialbe in the url. I left the regex one. just wanted to update you about it. – ha-neul Nov 30 '20 at 04:53
0

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.

Saijal Shakya
  • 122
  • 3
  • 9