views:

39

answers:

1

I'm trying to create a url pattern that will behave like controller/action/id route in rails. So far here is what I have :

from django.conf.urls.defaults import *
import views

urlpatterns = ('',
              (r'^(?P<app>\w+)/(?P<view>\w+)/$', views.select_view),
              )

Here is my 'views.py':

def select_view(request, app, view):
    return globals()['%s.%s', % (app, view,)]()

So far this hasn't worked. I get a key error exception in the 'globals' function. Am I going in the right direction here?

+1  A: 

Try something like this:

from django.utils.importlib import import_module

def select_view(request, app, view):
    mod = import_module('%s.views' % app)
    return getattr(mod, view)(request)

It is obviously oversimplified example, what you do is import views.py from your app and see if it has view function, and if it does execute that function giving request as the first argument.

See some examples of how Django does it with get_callable and autodiscover methods.

rebus