Django autoroute
With ASP.NET MVC, I can just add an action to 开发者_Go百科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?
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
and POST
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
:
from django.conf.urls.defaults import *
import myapp.views
urlpatterns = patterns('myapp.views',
*map(lambda x: url(r'^myapp/%s/$' % x, x, name='myapp_%s' % x),
[k for k,v in myapp.views.__dict__.items() if callable(v)]))
精彩评论