Asp.Net MVC style routing in Django
I've been programming in Asp.Net MVC for quite some time now and to expand a little bit beyond the .Net world I've recently began learning Python and Django. I am enjoying Django but one thing I am missing from Asp.Net MVC is the automatic routing from my urls to my controller actions.
In Asp.Net MVC I can build much of my application using this single default route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new 开发者_运维技巧{ controller = "Home", action = "Index", id = "" } // Parameter defaults
);
In Django I've found myself adding an entry to urls.py for each view that I want to expose which leads to a lot more url patterns than I've become used to in Asp.Net MVC.
Is there a way to create a single url pattern in Django that will handle "[Application]/view/[params]" in a way similar to Asp.Net MVC? Perhaps at the main web site level?
View may not only be function, but also a class.
You can easily specify some kind of DispatchedView class with __call__
method and dispatch to method according to remaining URI. Also, You can inspire yourself with CherryPy dispatcher.
However, it's considered beter to use named patterns and have URIs and views completely decoupled.
精彩评论