Re: calling the view function in the other view function

2012-12-01 Thread Chris Cogdon


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  
> 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/Zk05t9aEH-UJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: calling the view function in the other view function

2012-11-30 Thread Satinderpal Singh
On Sat, Dec 1, 2012 at 4:45 AM, Chris Cogdon  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,} )
>
>

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: calling the view function in the other view function

2012-11-30 Thread Chris Cogdon
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,} )


On Thursday, November 29, 2012 10:31:03 PM UTC-8, Satinderpal Singh wrote:
>
> 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/ 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/sv67A78gud4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.