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['redirect']!=0:
>           return add['redirect']
>
>      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 at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to