姜戈自动路线
使用 ASP.NET MVC,我只需向视图添加一个操作,它就会自动工作。 Django 似乎让我在 urls.py 表中写入每条路线 - 有没有办法让它映射,例如“/foo/bar”到 foo.views.bar
,而无需我明确说明?
With ASP.NET MVC, I can just add an action to a view and it will automagically work. Django seems to make me write every route in the urls.py table - is there a way to make it map, for example "/foo/bar" to foo.views.bar
without me explicitly saying so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 django 让你写所有东西的原因是这样的: “魔法”有什么问题“?
其次,您建议的地图使得处理视图函数的参数变得困难。最简单的方法是按照约定强制所有视图仅使用
GET
和POST
参数,否则采用一些标准参数集(例如request
、模板名称
)。要实现您想要的映射,您可以迭代视图模块并生成模式对象。请注意,这是一个非常丑陋的黑客行为,很大程度上违背了 url 映射器的目的。在 urls.py 中:
I think the reason django makes you write everything is something along these lines: What's wrong with "magic"?
Secondly, the map you are suggesting makes it difficult to deal with arguments to the view functions. The simplest would be to enforce by convention that all views only use
GET
andPOST
arguments and otherwise take some standard set of arguments (e.g.request
,template_name
).To implement this map you want, you can iterate over your views module and generate the patterns object. Mind you, this is a really ugly hack and largely defeats the purpose of the url mapper. In
urls.py
: