Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Returning value directly to another function [RSS Feed]

#1 Dec. 9, 2010 14:21:51

Q.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Returning value directly to another function


Hello, this is rather python problem, but maybe You can help. What i
want to do is to return value in another function calling from other
function.

It doesnt mean i want this:

def Func():
return 1

def Func2():
return Func()

I want function Func to return 1 directly in Func2 as it is Func2
returning it.

I have this problem because view function has to return response
object and my function that adds element to database (comments)
returns HttpResponseRedirect or context dict with form that has
errors.
If it returns redirect then i want view function to return it, if it
return context then i dont want to return it but rather give it to
render_to_response.

It comes to this:

def Add():
#form valid, add to db
return { 'redirect': HttpResponseRedirect('url'), }

#form not valid
return { 'form': form, 'redirect': 0, }

def View(request):
add = Add()
if add!=0:
return add

return render_to_response(template,add)

Thing i dont want here is if statement checking if there is redirect,
so i want within function Add return HttpResponseRedirect as it is
View function.

I dont know if this is possible.





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

Offline

#2 Dec. 9, 2010 14:46:28

Ilian I.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Returning value directly to another function


Hi

what exactly is the problem with the if check?
Why is it so bad?

On Thu, Dec 9, 2010 at 4:21 PM, Quetzacotl <quetzaco...@gmail.com> wrote:

> Hello, this is rather python problem, but maybe You can help. What i
> want to do is to return value in another function calling from other
> function.
>
> It doesnt mean i want this:
>
> def Func():
> return 1
>
> def Func2():
> return Func()
>
> I want function Func to return 1 directly in Func2 as it is Func2
> returning it.
>
> I have this problem because view function has to return response
> object and my function that adds element to database (comments)
> returns HttpResponseRedirect or context dict with form that has
> errors.
> If it returns redirect then i want view function to return it, if it
> return context then i dont want to return it but rather give it to
> render_to_response.
>
> It comes to this:
>
> def Add():
> #form valid, add to db
> return { 'redirect': HttpResponseRedirect('url'), }
>
> #form not valid
> return { 'form': form, 'redirect': 0, }
>
> def View(request):
> add = Add()
> if add!=0:
> return add
>
> return render_to_response(template,add)
>
> Thing i dont want here is if statement checking if there is redirect,
> so i want within function Add return HttpResponseRedirect as it is
> View function.
>
> I dont know if this is possible.
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>


--
eng. Ilian Iliev
Web Software Developer

Mobile: +359 88 66 08 400
Website: ilian.i-n-i.org

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

Offline

#3 Dec. 10, 2010 02:22:13

R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Returning value directly to another function


On Dec 9, 9:21 am, Quetzacotl <quetzaco...@gmail.com> wrote:
> Hello, this is rather python problem, but maybe You can help. What i
> want to do is to return value in another function calling from other
> function.
>
> It doesnt mean i want this:
>
> def Func():
>      return 1
>
> def Func2():
>      return Func()
>
> I want function Func to return 1 directly in Func2 as it is Func2
> returning it.
>
> I have this problem because view function has to return response
> object and my function that adds element to database (comments)
> returns HttpResponseRedirect or context dict with form that has
> errors.
> If it returns redirect then i want view function to return it, if it
> return context then i dont want to return it but rather give it to
> render_to_response.
>
> It comes to this:
>
> def Add():
>      #form valid, add to db
>      return { 'redirect': HttpResponseRedirect('url'), }
>
>      #form not valid
>      return { 'form': form, 'redirect': 0, }
>
> def View(request):
>      add = Add()
>      if add!=0:
>           return add
>
>      return render_to_response(template,add)
>
> Thing i dont want here is if statement checking if there is redirect,
> so i want within function Add return HttpResponseRedirect as it is
> View function.
>
> I dont know if this is possible.

You can use isinstance() function to check if returned value is
an instance of some class, and make for simpler code:

obj = add()
if isinstance(Cls, obj): return obj
..
return render_to_response(template, obj)

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

Offline

  • Root
  • » Django
  • » Returning value directly to another function [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support