On Friday, November 30, 2012 9:22:08 PM UTC-8, Satinderpal Singh wrote:
On Sat, Dec 1, 2012 at 4:45 AM, Chris Cogdon <ch...@cogdon.org> wrote:
> It's generally very messy for one "view" function to call another.
Then what is the fun of making the function, if it is not reusable.
>
> Better solution is to factorise out the common parts into a third function,
> then make sure each of your two "view" functions have the required
> render_to_response.
What will i do if i have a large number of views having the common part.
Take the common part out of each view and put it in a non-view (ie, not connected from a URLConf) function, then each of your view functions calls it.
Eg:
def view1 ( request ):
val1, val2, val3 = do_common_stuff ( request )
# non-common stuff here
return render ( request, "templatename", vars )
def view2 ( request ):
val1, val2, val3 = do_common_stuff ( request )
# non common stuff here
return render ( request, "templatename2", vars )
def do_common_stuff ( request ):
# all your common code here
return computed_val1, val2, val3
In other words, unless you really know what you're doing, once you return the HttpResponse object, that should be the last thing you do.
Note, though. that if your "common code" wants to WRITE INTO the HttpResponse object, thats fine too... just create one and then pass it as a parameter to your common function.