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?
Asked
Active
Viewed 201 times
1

Xodarap
- 11,581
- 11
- 56
- 94
1 Answers
2
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)]))
-
Hmm, I would've thought this fits in with DRY. I'll try your hack. – Xodarap Dec 16 '10 at 15:17