开发者

Is it possible to route to a particular view based on URL variables passed?

开发者 https://www.devze.com 2023-01-30 00:23 出处:网络
I\'m doing some kind of complex routing in Django. I have a collection of apps where which view will depend on which URL variables are passed.

I'm doing some kind of complex routing in Django. I have a collection of apps where which view will depend on which URL variables are passed.

For example, in my URL I have a profile slug and a module slug. If the logged user has permission, they'll see whichever profile depending on the profile slug in the URL. If they have permission, they'll see which module depending on the module slug in the URL and if it's a child of the selected profile.

The modules are basically different apps. One of the modules is a tasks开发者_如何学C list and another is a media list etc. Because the user can have multiple tasks modules or multiple media modules, each with their own slug, I need a way of routing to the task or media view depending on what the URL is.

For example:

http://127.0.0.1:8000/james-morris/wedding/ --- this will go to the profile of 'james-morris' and the module 'wedding' which happens to be a tasks list and needs to go to the tasks view

http://127.0.0.1:8000/james-morris/icons/ --- this will go to the profile of 'james-morris' and the module 'icons' which happens to be a media list and needs to go to the media view

How would I approach doing this sort of routing in Django? Many thanks, James


Perhaps I'm mistaken, but I believe that something like this would already do it.

def redirect(request, username, module):
    '''do some stuff here...'''
    if username.has_icons():
        return show_icon(request, username)
    #elif ... something else...

urlpatterns = patterns('',
    (r'^(?P<username>\w+)/(?P<module>\w+)/$', redirect),
)


Is it possible to route to a particular view based on URL variables passed ?
The cleanest way to perform routing would be by respecting the basic REST rule - One URL per resource. So, the best thing to do would be to land in the view on the basis of the URL and then you can create an if..elif..else.. block to check for various variables passed and route accordingly to the different view(s).

As for the examples you have mentioned, Django should route you to different views, if you don't target the same view in your urls.py


As a variation on Sidharth Sharma's solution, if you need to check permissions then another option is to have an app specifically for doing the rerouting. The view in this app then does a HttpResponseRedirect to the URL specific to the app you want.

urlpatterns = patterns('',
    (r'^(?P<username>\w+)/?P<option>/$', 'reroute.views.direct'),
    (r'^(?P<username>\w+)/wedding/$', 'wedding.views.tasks'),
    (r'^(?P<username>\w+)/icons/$', 'icons.views.media'),
)

def direct(request, username, option):

   if has_condition_for_wedding:
       HttpResponseRedirect('/username/wedding/')
   else:
       HttpResponseRedirect('/username/icons/')
0

精彩评论

暂无评论...
验证码 换一张
取 消