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
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
--
AMV Multiviajes Argentina
Esmeralda 847 Piso 12 Of. 'G'
C1007ABI - Buenos Aires
Argentina
Tel: +54 11 5031-3060 / 3061
Fax: +54 11 4313-6141
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
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
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:
>
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
--