I can not get data from the database in Django

0

I have an error that has been happening to me for two days, which I do not understand what it may be, is when I try to obtain the information from the database with the employee model, the code is as follows:

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.core.exceptions import ObjectDoesNotExist
from django.views.generic.list import ListView

from apps.empleado.forms import EmpleadoForm
from apps.empleado.models import Empleado

# Create your views here.
def index(request):
    return render(request, 'empleado/index.html')

def empleado_view(request):
    if request.method == 'POST':
        form = EmpleadoForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('/empleado')
    else:
        form = EmpleadoForm()

    return render(request, 'empleado/alta.html', {'form':form})

def empleado_list(request):
    empleado = Empleado.objects.all()
    return render(request, 'empleado/index.html', {'empleados':empleado})

class EmpleadoList(ListView):
    model = Empleado
    template_name='empleado/index.html'

and in error that appears to me is the following;

could someone help me what could be this error ?, intenet using something called django-lint but it seems that I configure it wrong or it will not be the solution, I'm using Django 2.0.2 with the following libraries installed via pip:

asn1crypto==0.24.0
astroid==1.6.2
attrs==17.4.0
Automat==0.6.0
cffi==1.11.5
colorama==0.3.9
constantly==15.1.0
cryptography==2.1.4
cssselect==1.0.3
dj-database-url==0.5.0
Django==2.0.2
django-filter==1.1.0
django-material==1.2.2
django-materialize-css==0.0.1
django-report-builder==4.0.3
django-reset-migrations==0.3.1
django-viewflow==1.2.2
djangorestframework==3.7.7
et-xmlfile==1.0.1
gunicorn==19.7.1
hyperlink==18.0.0
idna==2.6
incremental==17.5.0
isort==4.3.4
jdcal==1.3
lazy-object-proxy==1.3.1
lxml==4.1.1
mccabe==0.6.1
mysqlclient==1.3.12
numpy==1.14.1
openpyxl==2.5.0
parsel==1.4.0
Pillow==5.0.0
psycopg2==2.7.4
pyasn1==0.4.2
pyasn1-modules==0.2.1
pycparser==2.18
pylint==1.8.3
pyOpenSSL==17.5.0
python-dateutil==2.6.1
pytz==2018.3
reportlab==3.4.0
service-identity==17.0.0
six==1.11.0
w3lib==1.19.0
whitenoise==3.3.1
wrapt==1.10.11
zope.interface==4.4.3

I'm not using all of them but I wanted to put them all together in case I missed any, thanks for your help I hope you can help me

PS: in the Django shell the methods to save, consult, modify and delete the data work well, it just does not work to get them when I'm using them in the view

Edit1: this is the code of the model used

from django.db import models
from django.core.validators import RegexValidator 

#from apps.estado.models import Estado
#from apps.sucursal.models import Sucursal
#from apps.kernel.models import Estatus #Error de compilador por alguna razon

# Create your models here.
class Genero(models.Model):
    genero = models.CharField(max_length=9)
    created_at = models.DateField(auto_now_add=True)
    updated_at = models.DateField(auto_now=True)

    def __str__(self):
        return '{}'.format(self.genero)

class TipoEmpleado(models.Model):
    nombre = models.CharField(max_length=25)
    created_at = models.DateField(auto_now_add=True)
    updated_at = models.DateField(auto_now=True)

    def __str__(self):
        return '{}'.format(self.nombre)

class Empleado(models.Model):
    #emplado_id = models.IntegerField(primary_key=True)
    no_reloj = models.CharField(max_length=4, unique=True)
    nombre = models.CharField(max_length=40) #40
    apellido_paterno = models.CharField(max_length=25) #25
    apellido_materno = models.CharField(max_length=25, null=True) #25
    genero = models.ForeignKey('empleado.Genero', null=True, blank=True, on_delete=models.CASCADE)
    email = models.EmailField(max_length=120, null=True, unique=True) #120
    curp = models.CharField(max_length=18, null=True, unique=True) #18
    rfc = models.CharField(max_length=13, null=True, unique=True) #13
    nss = models.CharField(max_length=11, null=True, unique=True) #11
    direccion = models.CharField(max_length=120, null=True) #120
    sucursal = models.ForeignKey('sucursal.Sucursal', null=True, blank=True, on_delete=models.CASCADE)
    estado = models.ForeignKey('estado.Estado', null=True, blank=True, on_delete=models.CASCADE) #6
    fecha_alta = models.DateField(null=True)
    estatus = models.ForeignKey('kernel.Estatus', null=True, blank=True, on_delete=models.CASCADE)
    fecha_nac = models.DateField(null=True)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

    def __str__(self):
        return '(%s) %s %s' % (self.no_reloj, self.nombre, self.apellido_paterno)

This is the code of the index.html page:

{% extends 'base/base.html' %}

{% block titleApp %}
    Empleados
{% endblock titleApp %}


{% block content %}
<div class="mdl-grid">
    <div class="mdl-cell mdl-cell--1-col"></div>
    <div class="mdl-cell mdl-cell--10-col">

        <!-- Iniciamos una lista de los empleados a mostrar -->
        <ul class="demo-list-two mdl-list">

            {% if empleados %}
            {% for empleado in empleados %}

                <li class="mdl-list__item mdl-list__item--two-line">
                    <span class="mdl-list__item-primary-content">
                        <i class="material-icons mdl-list__item-avatar">person</i>
                        <span>({{ empleado.no_reloj }}) {{ empleado.nombre }}</span>
                        <span class="mdl-list__item-sub-title">
                            <a id="email_info" class="mdl-navigation__link" href=""><i class="material-icons">email</i>[email protected]</a> <a id="phone_info" class="mdl-navigation__link" href=""><i class="material-icons">call</i>(656)123-1234</a>
                        </span>
                    </span>
                    <span class="mdl-list__item-secondary-content">
                        <span class="mdl-list__item-secondary-info">Editar</span>
                        <a class="mdl-list__item-secondary-action" href="#"><i id="edit_person" class="material-icons">mode_edit</i></a>
                    </span>
                </li>
            {% endfor %}
            {% else %}
                <h1>No hay empleados registrados.</h1>
            {% endif %}
        </ul>
        <!-- Termina la lista de empleados de la BD -->
        <div class="mdl-tooltip mdl-tooltip--left" data-mdl-for="edit_person">
            Editar informacion <br/> de este empleado.
        </div>
        <div class="mdl-tooltip mdl-tooltip--right" data-mdl-for="input_field">
            Agrega un texto.
        </div>
        <div class="mdl-tooltip mdl-tooltip--right" data-mdl-for="email_info">
            Correo de contacto.
        </div>
        <div class="mdl-tooltip mdl-tooltip--right" data-mdl-for="phone_info">
            Telefono de contacto.
        </div>
    </div>
    <div class="mdl-cell mdl-cell--1-col"></div>
</div>

{% endblock content %}

Edit2: add the full coding of view.py

    
asked by Fernando A. León 11.04.2018 в 21:25
source

1 answer

0

try putting the import inside the function in case you are changing the value by some part

def empleado_list(request):
    from apps.empleado.models import Empleado
    empleado = Empleado.objects.all()
    return render(request, 'empleado/index.html', {'empleados':empleado})

The other an import is usually done as follows:

from empleado.models import Empledo

try doing the import, if this works, you may have poorly structured your project

The other, make sure you have added your apps to INSTALLED_APPS in setting.py

INSTALLED_APPS = [
    .....
    'empleado', # aunque en tu caso podria ser 'apps.empleado'
] 
    
answered by 11.04.2018 в 23:22