Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django dynamic urlpatterns

Tags:

python

django

I'm building a simple web application with Django. My users are separated into multiple groups, for example Group A, Group B, etc.

What I want to do is to dynamically update urlpatterns list in urls.py so that I can have different views on same url endpoints.

For example, I'd like to do something like this (I know syntax is off, it's just to demonstrate what I want)

urlpatterns = [
    url(r'^$', views.homepage, name='homepage'),
    url(r'^login/$', views.BaseLogin.as_view(), name='core.login'),
    url(r'^logout/$', views.logout, name='core.logout'),
]  + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if request.user in groupA:
    urlpatterns.append(url(r'^dash/', include('groupA.urls')))
else:
    urlpatterns.append(url(r'^dash/', include('groupB.urls')))

How would I best achieve this?

like image 936
intelis Avatar asked May 31 '26 19:05

intelis


1 Answers

I think this is neither possible nor desirable. You should place such logic in the view. Make both land in the same view and redirect or simply put together different content in the view based on the user's group affiliation.

like image 113
user2390182 Avatar answered Jun 02 '26 09:06

user2390182