Django 中的 Rails 或 web2py 风格 URL 路由
我是姜戈的新手。似乎 Django 需要为每个控制器/操作(Django 术语中的视图/函数)定义一个 URL 映射规则。实现 Rails 风格的 URL 路由或至少是 web2py 风格的 URL 路由的最简单方法是什么?
对于那些不知道什么是 Rails 或 web2py 样式 URL 路由的人。
- Rails RESTful URL 路由
- web2py URL 路由:
http: //domain.com/C/A
自动映射到 C 控制器 A 函数。
I'm new to Django. Seems that Django requires to define one URL mapping rule for each controller/action (view/function in Django's term). What's the easiest way to implement Rails-style URL routing, or at least web2py-style URL routing?
For those who don't know what is Rails or web2py style URL routing.
- Rails RESTful URL routing
- web2py URL routing:
http://domain.com/C/A
maps to C controller A function automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要做的事情明显违背了 Django 的设计理念:“将 URL 与 Python 函数名称绑定是一件糟糕而丑陋的事情。”所以我认为你有两个简单的选择和一个困难的选择。
简单#1:坚持使用rails
简单#2:按照 django 的正常使用方式使用 django:每个视图一个 URL 规则。
困难:编写一个通用的 URL 调度程序,使用自省来根据 URL 查找视图方法。请务必小心确保其安全。完成后分享。 :)
并不是说你不应该这样做。但如果您正在寻找一种简单的方法来做到这一点,我怀疑您会感到沮丧。
What you're trying to do goes explicitly against Django's design philosophy: "Tying URLs to Python function names is a Bad And Ugly Thing." So I think you have two easy choices and one hard one.
Easy #1: Stick with rails
Easy #2: Use django the way django is normally used: one URL rule per view.
Difficult: Write a generic URL dispatcher that uses introspection to look up view methods based on URLs. Be very careful making it secure. And share it when you're done. :)
Not saying you shouldn't do it. But if you're looking for an easy way to do this I suspect you'll be frustrated.
您可以以自定义方式轻松编写它 - 编写一个接受 url 并解析它的视图。然后它加载你想要的模块。那么 urls.py 文件中将只有一条规则,该规则将仅调用一个视图函数。
你也可以创建中间件来解决你遇到的问题......
但我不建议这样做,我认为最好坚持 Django 使用的路由风格。为什么?因为稍后您将不得不考虑自定义 url 解析器,因此您将无法使用例如 {% url %} 模板标记(同时您也无法自定义它)。我认为不值得在这里重新发明一些东西......但这只是我的意见,由你决定:)
玩得开心:)
You can write it easily in custom way - write one view which accepts the url and parses it. then it loads module you want. Then you will have only one rule in urls.py file, which will call just one view function.
Also you can create middleware probably to solve the issue you have....
But I wouldn't recommend to do so and I think it is best to stick with the style of routing Django uses. Why? Because later then you will have to think about custom url resolvers, you will not be able to use for example {% url %} template tag (while you will not customize it also). I think it is not worth to reinvent something here... But this is only my opinion, it is you who decides :)
Have fun :)
我建议查看 Django URL 调度程序 的文档。在那里你应该有很多方法来解决你的问题。但是,如果您正在寻找以更快的方式定义 URL 的快速答案,那么我会看看 视图前缀 它们减少了您可能会看到的一些膨胀。
如果您正在研究 REST,那么我建议您看一下这篇文章。
I would recommend looking over the documentation for the Django URL dispatcher. There you should a bunch of ways to solve your problem. However if you are looking for a quick answer on a faster way to define your URL's then I would take a look at View Prefixes they cut down some of the bloat that you may be seeing.
If you are looking at REST then I would recommend taking a look at this article.