是否可以根据传递的 URL 变量路由到特定视图?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许我错了,但我相信这样的事情已经可以做到了。
Perhaps I'm mistaken, but I believe that something like this would already do it.
是否可以根据传递的 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
作为 Sidharth Sharma 解决方案的变体,如果您需要检查权限,那么另一个选择是使用专门用于重新路由的应用程序。然后,此应用程序中的视图对特定于您想要的应用程序的 URL 执行 HttpResponseRedirect。
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.