Calling a view from another view in the same app?

3,090 views
Skip to first unread message

Facundo Casco

unread,
Sep 5, 2006, 6:42:00 PM9/5/06
to django...@googlegroups.com
Hi, maybe I'm doing something wrong (I'm just starting to learn Python and Django), I need to call a view (viaje) from another view in the same app but I get this error:

UnboundLocalError at /adm/viaj/
  local variable 'viaje' referenced before assignment

The relevant part of the code is

amxdev/contable/views.py
...

def viajes(request):
    if request.GET['numero'] != '':
       resp = viaje(request)
       return resp


def viaje(request, numero=None):
    ...
    return render_to_response(...)



Thanks for your help.


Facundo
facundo.vcf

arthur debert

unread,
Sep 5, 2006, 7:08:27 PM9/5/06
to Django users
Hi facundo.

this is an issue in python that is pretty confusing for newbies.

What's happening here is that viajes is making a forward reference to
viaje, meaning that when viajes is run viaje has not been defined yet,
so it throws an exception(methods get parsed top to bottom in modules).

The fix for this is to put the 'viajes' method after 'viaje' on your
file.

cheers
arthur

Facundo Casco

unread,
Sep 5, 2006, 7:26:10 PM9/5/06
to django...@googlegroups.com
Arthur, thanks for your replay. I've changed the definitions order but I
still get the same error.

Facundo

--
AMV Multiviajes Argentina
Esmeralda 847 Piso 12 Of. 'G'
C1007ABI - Buenos Aires
Argentina
Tel: +54 11 5031-3060 / 3061
Fax: +54 11 4313-6141

facundo.vcf

Alan Green

unread,
Sep 5, 2006, 7:39:41 PM9/5/06
to django...@googlegroups.com
On 9/6/06, Facundo Casco <fac...@multiviajesar.com.ar> wrote:
>
> Hi, maybe I'm doing something wrong (I'm just starting to learn Python and
> Django), I need to call a view (viaje) from another view in the same app but
> I get this error:
>
> UnboundLocalError at /adm/viaj/
> local variable 'viaje' referenced before assignment

You have an assignment to a local variable named 'viaje', but you are
using the name 'viaje' before that assignment. Look for a line that
begins "viaje =". It's best not to have variables and functions with
the same name.

If you're still having trouble, please post the last ten or fifteen
lines of the stacktrace, plus the complete code for the function with
the UnboundLocalError, and someone will be able to pinpoint the
problem for you.

Regards,

Alan.

>
> The relevant part of the code is
>
> amxdev/contable/views.py
> ...
>
> def viajes(request):
> if request.GET['numero'] != '':
> resp = viaje(request)
> return resp
>
>
> def viaje(request, numero=None):
> ...
> return render_to_response(...)
>
>
>
> Thanks for your help.
>
>
> Facundo
> >
>
>


--
Alan Green
al...@bright-green.com - http://bright-green.com

Facundo Casco

unread,
Sep 5, 2006, 8:47:07 PM9/5/06
to django...@googlegroups.com

>> UnboundLocalError at /adm/viaj/
>> local variable 'viaje' referenced before assignment
>>
>
> You have an assignment to a local variable named 'viaje', but you are
> using the name 'viaje' before that assignment. Look for a line that
> begins "viaje =". It's best not to have variables and functions with
> the same name.
>
> If you're still having trouble, please post the last ten or fifteen
> lines of the stacktrace, plus the complete code for the function with
> the UnboundLocalError, and someone will be able to pinpoint the
> problem for you.
>
> Regards,
>
> Alan.
>
Thanks for your help, this is the code, what I want to do is call the
function viaje defined before. I can use that same function if I call it
from my urls.py but it doesn't work from the views.py file.

def viaje(request, numero=None):
+-- 37 lines: "Genera un informe detallado de un viaje"---------

def viajes(request):
"Genera listados de viajes por distintos criterios"
clie = 'Todos'
prov = 'Todos'
desde = 'Sin limite'
hasta = 'Sin limite'
paxs = 'Todos'
viajes = amxdev.vtour.models.viaje().getAllObjects()


if request.GET['numero'] != '':
resp = viaje(request)
return resp

Facundo

facundo.vcf

Alan Green

unread,
Sep 5, 2006, 9:14:10 PM9/5/06
to django...@googlegroups.com
Hi Facundo,

Nope the problem's not in that code. The stack trace will tell you the
method with the problem.

Just in case you didn't know, UnboundLocalError is a Python error that
you get when a method refers to a variable before assigning it a
value. For example:

>>> def f():
... print q
... q = 2
...
>>> f()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in f
UnboundLocalError: local variable 'q' referenced before assignment
>>>

Hope that helps. If not, please post the full stack trace of the error.

Alan.


On 9/6/06, Facundo Casco <fac...@multiviajesar.com.ar> wrote:
>

Facundo Casco

unread,
Sep 6, 2006, 11:44:55 AM9/6/06
to django...@googlegroups.com
Alan Green wrote:
> Hi Facundo,
>
> Nope the problem's not in that code. The stack trace will tell you the
> method with the problem.
>
> Just in case you didn't know, UnboundLocalError is a Python error that
> you get when a method refers to a variable before assigning it a
> value. For example:
>
>
>>>> def f():
>>>>
> ... print q
> ... q = 2
> ...
>
>>>> f()
>>>>
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> File "<interactive input>", line 2, in f
> UnboundLocalError: local variable 'q' referenced before assignment
>
>
> Hope that helps. If not, please post the full stack trace of the error.
>
> Alan.
>

Alan, thanks for your help. This is the complete error reported by Django:


|Traceback (most recent call last):

File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in
get_response
74. response = callback(request, *callback_args, **callback_kwargs)
File "/home/facundo/amxdev/contable/views.py" in viajes
484. resp = viaje(request)

UnboundLocalError at /adm/viaj/
local variable 'viaje' referenced before assignment

'viaje' should be referencing the method defined before, not a variable.
As you can see, the code should be in views.py line 484

I've tried changing that line to:
resp = amxdev.contable.views.viaje(request)

and now it works. I though that Python would look in the same file for
definitions, actually in other app's models file I define a function at
the end of the file and use it in the middle inside some class' method
without any problems.
Anyway, thanks again for your time and help.

Regards.

Facundo

--

facundo.vcf
Reply all
Reply to author
Forward
0 new messages