Question

In my Django project, my url.py module looks something like this:

urlpatterns = patterns('',
    (r'^$', 'web.views.home.index'),
    (r'^home/index', 'web.views.home.index'),
    (r'^home/login', 'web.views.home.login'),
    (r'^home/logout', 'web.views.home.logout'),
    (r'^home/register', 'web.views.home.register'),
)

Is there a way to simplify this so that I don't need an entry for every method in my view? Something like this would be nice:

urlpatterns = patterns('',
    (r'^$', 'web.views.home.index'),
    (r'^home/(?<method_name>.*)', 'web.views.home.(?P=method_name)'),
)

UPDATE

Now that I know at least one way to do this, is this sort of thing recommended? Or is there a good reason to explicitly create a mapping for each individual method?

Was it helpful?

Solution

May be something like that:

import web.views.home as views_list
urlpatterns = patterns('',
    (r'^$', 'web.views.home.index'),
    *[(r'^home/%s' % i, 'web.views.home.%s' % i) for i in dir(views_list)]
)

OTHER TIPS

You could use a class-based view with a dispatcher method:

class MyView(object):
    def __call__(self, method_name):
        if hasattr(self, method_name):
            return getattr(self, method_name)()


    def index(self):
        ...etc...

and your urls.py would look like this:

from web.views import MyView
urlpatterns = patterns('',
    (r'^$', 'web.views.home.index'),
    (r'^home/(?<method_name>.*)', MyView()),
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top