是否可以根据传递的 URL 变量路由到特定视图?

发布于 2024-10-06 23:51:56 字数 728 浏览 8 评论 0原文

我正在 Django 中进行某种复杂的路由。我有一组应用程序,其中哪个视图将取决于传递的 URL 变量。

例如,在我的 URL 中,我有一个配置文件 slug 和一个模块 slug。如果登录的用户具有权限,他们将根据 URL 中的配置文件 slug 查看任何配置文件。如果他们有权限,他们将根据 URL 中的模块 slug 以及它是否是所选配置文件的子项来查看哪个模块。

这些模块基本上是不同的应用程序。其中一个模块是任务列表,另一个是媒体列表等。因为用户可以拥有多个任务模块或多个媒体模块,每个模块都有自己的子集,所以我需要一种根据内容路由到任务或媒体视图的方法网址是。

例如:

http://127.0.0.1:8000/james-morris/wedding/ --- 这将转到“james-morris”的个人资料和模块“wedding”,该模块恰好是一个任务列表,需要转到任务视图

http://127.0.0.1:8000/james-morris/icons/ --- 这将转到“james-morris”的配置文件和模块“icons”,它恰好是一个媒体列表,需要转到媒体视图

我将如何在 Django 中进行这种路由? 非常感谢,詹姆斯

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃颗糖壮壮胆 2024-10-13 23:51:56

也许我错了,但我相信这样的事情已经可以做到了。

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),
)

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),
)
黑凤梨 2024-10-13 23:51:56

是否可以根据传递的 URL 变量路由到特定视图?
执行路由的最简洁方法是遵守基本的 REST 规则 - 每个资源一个 URL。因此,最好的办法是根据 URL 登陆视图,然后您可以创建一个 if..elif..else.. 块来检查传递的各种变量和相应地路由到不同的视图。

至于您提到的示例,如果您不在 urls.py 中定位相同的视图,Django 应该将您路由到不同的视图

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

七颜 2024-10-13 23:51:56

作为 Sidharth Sharma 解决方案的变体,如果您需要检查权限,那么另一个选择是使用专门用于重新路由的应用程序。然后,此应用程序中的视图对特定于您想要的应用程序的 URL 执行 HttpResponseRedirect。

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/')

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/')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文