calling the view function in the other view function

48 views
Skip to first unread message

Satinderpal Singh

unread,
Nov 30, 2012, 1:31:03 AM11/30/12
to django...@googlegroups.com
I have to call a function into another function which displays the
values from the database to the browser, like this:

def result(request):
zee = head.objects.aggregate(Max('id'))
mee = zee['id__max']
Head = head.objects.filter(id = mee)
return render_to_response('report/report_base.html',
{'Head':Head,},context_instance=RequestContext(request))

def result_cube(request):
Head = result(mee)
organisation = Organisation.objects.all().filter(id = 1)
return render_to_response('report/cube.html', { 'Head':Head,
'organisation':organisation},context_instance=RequestContext(request))

I want to call "result" view in the "result_cube", as it displays the
html data but not showing the data from the database.

--
Satinderpal Singh
http://satindergoraya.blogspot.in/
http://satindergoraya91.blogspot.in/

Chris Cogdon

unread,
Nov 30, 2012, 6:15:35 PM11/30/12
to django...@googlegroups.com
It's generally very messy for one "view" function to call another.

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.

Also, I highly recommend using the "render" shortcut, which would turn your more complex calls into this:

return render ( request, 'report/report_base.html', {'Head':Head,} )

Satinderpal Singh

unread,
Dec 1, 2012, 12:22:08 AM12/1/12
to django...@googlegroups.com
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.

> Also, I highly recommend using the "render" shortcut, which would turn your
> more complex calls into this:
Thanks, for the kind advise.
> return render ( request, 'report/report_base.html', {'Head':Head,} )
>
>

Chris Cogdon

unread,
Dec 1, 2012, 4:20:19 AM12/1/12
to django...@googlegroups.com


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.
Reply all
Reply to author
Forward
0 new messages