models.py
from django.db import models
from ckeditor.fields import RichTextField
#
from applications.departamento.models import Departamento
class Habilidades(models.Model):
hablidad = models.CharField('Habilidad', max_length=50)
class meta:
verbose_name = 'Habilidad'
verbose_name_plural = 'Habilidades Empleados'
def __str__(self):
return str(self.id) + '-' + self.hablidad
# Create your models here.
class Empleado(models.Model):
""" Modelo para tabla empleado """
JOB_CHOICES = (
('0', 'CONTADOR'),
('1', 'ADMINISTRADOR'),
('2', 'ECONOMISTA'),
('3', 'OTRO'),
)
first_name = models.CharField('Nombres', max_length=60)
last_name = models.CharField('apellidos', max_length=60)
full_name = models.CharField(
'Nombres completos',
max_length=120,
blank=True
)
job = models.CharField('Teabajo', max_length=1, choices=JOB_CHOICES)
departamento = models.ForeignKey(Departamento, on_delete=models.CASCADE)
avatar = models.ImageField(upload_to='empleado', blank=True, null=True)
habilidades = models.ManyToManyField(Habilidades)
hoja_vida=RichTextField()
class Meta:
verbose_name = 'Mi Empleado'
verbose_name_plural = 'Empleados de la empresa'
ordering = ['-first_name', 'last_name']
unique_together = ('first_name', 'departamento')
def __str__(self):
return str(self.id) + '-' + self.first_name + '-' + self.last_name
views.py
from django.urls import reverse_lazy
from django.views.generic import (
ListView,
DetailView,
CreateView,
TemplateView,
UpdateView,
DeleteView
)
# models
from .models import Empleado
# forms
from .forms import EmpleadoForm
class ListAllEmpleados(ListView):
template_name = 'persona/list_all.html'
paginate_by = 4
ordering = 'first_name'
context_object_name = 'empleados'
def get_queryset(self):
palabra_clave = self.request.GET.get("kword", '')
lista = Empleado.objects.filter(
full_name__icontains=palabra_clave
)
return lista
urls.py
from django.contrib import admin
from django.urls import path
from . import views
app_name = "persona_app"
urlpatterns = [
path(
'listar-todo-empleados/',
views.ListAllEmpleados.as_view(),
name='empleados_all'
),
]
templates/persona.list_all.html
{% extends 'base.html' %}
{% block content %}
{% include "includes/header.html" %}
<div class="grid-container">
<div class="grid-x">
<h1 class="cell">Lista Empleados</h1>
<form class="cell grid-x grid-margin-x" method="GET">{% csrf_token %}
<div class="cell large-7">
<input type="text" id="kword" name="kword" placeholder="buscar empleado">
</div>
<div class="cell large-2">
<button type="submit" class="success button">Buscar</button>
</div>
</form>
<div class="cell">
<table>
<thead>
<tr>
<th width="200">ID</th>
<th>NOMBRES</th>
<th width="150">APELLIDOS</th>
<th width="150">DEPARTAMENTO</th>
<th width="150">ACCION</th>
</tr>
</thead>
<tbody>
{% for e in empleados %}
<tr>
<td>{{ e.id }}</td>
<td>{{ e.first_name }}</td>
<td>{{ e.last_name }}</td>
<td>{{ e.departamento }}</td>
<td><a class="button warning" href="{% url 'persona_app:empleado_detail' e.id %}">Ver</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="cell">
{% if is_paginated %}
<nav aria-label="Pagination">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="pagination-previous">
<a href="?page={{page_obj.previous_page_number}}">Atras</a>
</li>
{% endif %}
{% for pagina in paginator.page_range %}
{% ifequal pagina page_obj.number %}
<li class="current"><span class="show-for-sr">You're on page</span> {{ pagina }}</li>
{% else %}
<li class=""><a href="?page={{pagina}}">{{ pagina }}</a></li>
{% endifequal %}
{% endfor %}
{% if page_obj.has_next %}
<li class="pagination-next">
<a href="?page={{page_obj.next_page_number}}">Siguiente</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
</div>
</div>
{% endblock content %}

Última modificación: 05 Jan 2023 19:38
No hay comentarios aún
Sé el primero en comentar este artículo