0

我有一个问题,我需要通过复选框将列表发送到我的数据库,但问题是我在分配错误之前引用了局部变量 'medicinas',我不知道出了什么问题,这是我的观点。

视图.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
4

2 回答 2

0

好吧,你在 else statmant 和 inside 下创建了 'medicinas' solicitud_laboratorio。每一项都是本地作业。尝试在函数之外创建它或使用global medicinasinstand。

于 2013-09-30T19:15:01.230 回答
0

如果是真的,view.py你只是在初始化。将初始化移到块外。medecinasdatos.is_valid()if

于 2013-09-30T19:17:16.737 回答