2013-09-30 18 views
0

我有一個問題我需要發送一個列表到我的數據庫,我通過複選框,但問題是,我得到本地變量'medicinas'之前引用賦值錯誤,我不知道什麼是錯,這是我的觀點。變量''引用前賦值錯誤,使用django中的複選框

View.py

def solicitud_laboratorio(request): 
    if request.method == 'POST': 
     datos = Solicitud_lab(request.POST) 
     if datos.is_valid(): 
      print 'inside if Datos' 
      datos.save() 
      medicinas = [] 
     for i in range(len(request.POST.getlist('examen'))): 
      print i 
      data = { 
      'nombre_examen': request.POST.getlist('examen')[i], 
      'credencial': request.POST['laboratorio_credencial'] 

     } 
     print data 
     medicina = examenlab_form(data) 

     if medicina.is_valid(): 
      medicina.save() 
      medicinas.append(medicina) 
      messages.success(request, 'Alta Exitosa!') 
     return HttpResponseRedirect('') 
    else: 
     messages.success(request, 'Se presento un error al dar de alta') 
else: 
    medicinas = [examenlab_form()] 
    datos = Solicitud_lab() 
return render_to_response('sol_laboratorio.html', {'medicinas': medicinas, 'datos': datos}, 
          context_instance=RequestContext(request)) 

sol_laboratorio-HTML

<form id='formulario' method='post' name="example form" enctype='multipart/form-data' action=''> {% csrf_token %} 
    <table style="width:95%;" align="center" width="100%" cellspacing="0" cellpadding="6" border="0"> 
    <div style="text-align:center; font-style: oblique; color: red"> 
     {% if messages %} 
      <ul class="messages"> 
       {% for message in messages %} 
         <li{% if message.tags %} class="{{ message.tags }}"{% endif %}><b>  {{ message }}</b></li> 
       {% endfor %} 
      </ul> 
     {% endif %} 
    </div> 
    <tr> 
     <th align="center" bgcolor="#85D5EF" colspan="7">Solicitud Estudios de Laboratorio</th> 
    </tr> 
    <tr> 
     <br> 
     <td><label>Fecha:</label> 
     <td> 
      <output><b>{% now "D d M Y" %}</b></output> 
     </td> 
     <td>{{ formulario.medico_solicita_2.errors }} <label>Nombre del Medico Que Solicita: </label></td> 
     <td>{{ formulario.medico_solicita_2 }} </td> 
     <td><label>Credencial:</label></td> 
     <td><input name="laboratorio_credencial" class="forminput" 
        id={{ formulario.credencial_consultainicial }}</input></td> 
    </tr> 
    <tr> 
     <td><label>Nombre:</label></td> 
     <td><input name="nombre_pac_3" id="nombre_pac_3"></input></td> 
     <td><label>Apellido:</label></td> 
     <td><input name="apellido_pac" id="apellido_pac"></input></td> 
     <td><label>Fecha De Nacimiento:</label></td> 
     <td><input name="fecha_nacimiento_5" id="fecha_nacimiento_5"></input></td> 
    </tr> 
    <tr> 
    </tr> 
    <tr> 
     <td><label>Edad:</label></td> 
     <td><input name="edad_miembro9" id="edad_miembro9"></input></td> 
     <td><label>Sexo:</label></td> 
     <td><input name="sexo_4" id="sexo_4"></input></td> 
    </tr> 
</table> 
<table style="width:95%;" align="center" width="95%" cellspacing="0" cellpadding="5" border="0"> 
    <tr> 
     <td align="center"><label>Impresion Diagnostica 1(ICD-10): </label> 
      <br/><input name="impresion_diagnostica" id="impresion_diagnostica"></input></td> 

     <td align="center"><label>Impresion Diagnostica 2(ICD-10): </label> 
      <br/><input name="impresion_diagnostica2" id="impresion_diagnostica2"></input></td> 

     <td align="center"><label>Impresion Diagnostica 3(ICD-10): </label> 
      <br/><input name="impresion_diagnostica3" id="impresion_diagnostica3"></input></td> 

     <td align="center"><label>Impresion Diagnostica 4(ICD-10): </label> 
      <br/><input name="impresion_diagnostica4" id="impresion_diagnostica4"></input></td> 
    </tr> 
</table> 

錯誤

UnboundLocalError at /laboratorio/ 
local variable 'medicinas' referenced before assignment 
Request Method: POST 
Request URL: http://localhost:8000/laboratorio/ 
Django Version: 1.4.3 
Exception Type: UnboundLocalError 
Exception Value:  
local variable 'medicinas' referenced before assignment 
Exception Location: C:\proyectomediexcel\expmedico\views.py in solicitud_laboratorio, line 265 
Python Executable: c:\Python27\python.exe 
Python Version: 2.7.5 
Python Path:  
['C:\\proyectomediexcel', 
'c:\\Python27\\lib\\site-packages\\setuptools-1.1.4-py2.7.egg', 
'c:\\Python27\\lib\\site-packages\\pip-1.1-py2.7.egg', 
'c:\\Python27\\lib\\site-packages\\django_pagination-1.0.7-py2.7.egg', 
'C:\\Windows\\system32\\python27.zip', 
'c:\\Python27\\DLLs', 
'c:\\Python27\\lib', 
'c:\\Python27\\lib\\plat-win', 
'c:\\Python27\\lib\\lib-tk', 
'c:\\Python27', 
'c:\\Python27\\lib\\site-packages'] 
Server time: lun, 30 Sep 2013 12:04:28 -0700 

回答

0

view.py如果datos.is_valid()爲真,則只是初始化medecinas。在if塊之外移動初始化。

+0

It works :) thanks – GioBot

0

嗯,你創建了下其他statmant和內部solicitud_laboratorio 'medicinas'。 每一個都是本地分配。 嘗試在函數之外創建它或使用global medicinas instand。

相關問題