unbound local error: local variable: referenced before assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laxad
    New Member
    • Apr 2010
    • 1

    unbound local error: local variable: referenced before assignment

    Hi guys,

    I am using django for my webapplication which is based in python.

    When I am submitting a form, using views.py wihch is

    Code:
    def cvdetails(request, id):
      cvdb = get_object_or_404(cvdb, id=id)   <-- [B]error happening here[/B]
      if request.method == 'POST':
        f = cvform(instance=cvdb, data=request.POST)
        if f.is_valid():
          f.save()
      else:
        f = cvform(instance=cvdb)
      return render_to_response('cm/cvdetails.html', context_instance=RequestContext(request, {'form': f}))
    i get this error

    Code:
     
    UnboundLocalError at /cm/1/cv/
    
    local variable 'cvdb' referenced before assignment
    
    Request Method: 	GET
    Request URL: 	http://lab.cpcstrans.com/cm/1/cv/
    Exception Type: 	UnboundLocalError
    Exception Value: 	
    
    local variable 'cvdb' referenced before assignment
    any idea as to why this is happening?

    cvdb that is being passed into get_object is globally defined. its one of my models from my app

    "from portal.cm.model s import cvdb"

    but i get a local error.

    any suggestions would be appreciated! thankkk you
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    This example illustrates the error:
    Code:
    >>> cvdb = 1
    >>> def f():
    ... 	cvdb = str(cvdb)
    ... 	
    >>> f()
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "<interactive input>", line 2, in f
    UnboundLocalError: local variable 'cvdb' referenced before assignment
    >>> def f():
    ... 	global cvdb
    ... 	cvdb = str(cvdb)
    ... 	return cvdb
    ... 
    >>> f()
    '1'
    >>> def f():
    ... 	return str(cvdb)
    ... 
    >>> f()
    '1'
    >>>
    Use the global statement or, better, use a different variable name and access cvdb read-only, in which case Python will look outside the scope of the function.

    Comment

    Working...