I have the view function in django that written like a dispatcher calling other functions depending on the variable in request.GET, like this:
action = ''
for act in ('view1', 'view2', 'view3', 'view4', ... ):
if act in request.GET:
action = act
break
...
if action == '':
response = view0(request, ...)
elif action == 'view1':
response = view1(request, ...)
elif action == 'view2':
response = view2(request, ...)
...
The global dispatcher function contains many variable initialization routines and these variables are then used in viewXX functions. Also some initialization code depends on what action is being called.
So I feed that this is bad view design but I don’t know how I can rewrite it?
Instead of sending a request to http://mysite.com/path/to/url?action=view0 and using urlpatterns like this:
You could send your requests to http://mysite.com/path/to/url/view0 and use urlpatterns like this:
And then have each of the sub views call an
initializefunction to get the common variables.Whether or not this is bad design depends on what you’re doing. In some cases, having a dispatcher makes more sense than splitting everything out.