python - Django state-based url routing -
hello!
i love django app developing route url response based on logic beyond url string.
i able take state of application (the logged in user , session, contents of database) account. mean 2 users visit same url, based on type of account have (for instance) receive different responses.
an example
say app has user
, admin
user classes. both visit http://some-domain.com/dashboard
, see entirely different things there. furthermore, there sub-urls beyond dashboard
/dashboard/comments
, /dashboard/friends
. again, these entirely different views depending on user class.
at present this:
(urls.py)
urlpatterns = [ url(r'^dashboard/', include([ url(r'^$', render_dashboard), url(r'^settings/$', render_dashboard_settings), url(r'^friends/$', render_dashboard_friends), ])), ]
the issue setup there no way take current user account. can still made work because can route kinds of user accounts same template, , use {% if ... %}
statements there provide different content different users. make name of template ought rendered dynamic, in call django.shortcuts.render
. neither of these things want. want able differentiate between users @ earlier level of url mapping.
my question
what do want extend functionality of url
method take state account, , route based on functionality. ideally, this:
urlpatterns = [ user_class_based_url(r'', { 'admin': include([ url(r'^dashboard/', include([ url(r'^$', render_admin_dashboard), url(r'^settings/$', render_admin_dashboard_settings), url(r'^friends/$', render_admin_dashboard_friends), ])), ]), 'user': include([ url(r'^dashboard/', include([ url(r'^$', render_user_dashboard), url(r'^settings/$', render_user_dashboard_settings), url(r'^friends/$', render_user_dashboard_friends), ])), ]), 'someotheruserclass': include([.....]), }), ]
now, different renderer functions being called same url, different user classes. unfortunately, user_class_based_url
have totally made up. ideally, user_class_based_url
custom function write. way write other similar functions take app's state account in other ways.
question: is there way can functionality?
in response "you shouldn't designing way"
the example have given contrived question across more quickly. want functionality have described because incredibly helpful in many ways app building, , sure better approach alternative.
note
i using django rest framework, in case can used implement design.
there isn't way in django @ moment. recently, there has been this discussion creating new api url dispatcher, changes in future version of django.
Comments
Post a Comment