I want to protect the view of a template, since I have several types of extended users with model User
of Django, I find it difficult to work. I have already tried it in many ways but without success, this is one and it gives me error.
This is the view:
def ViajesCliente(request, id_cliente):
if request.user.is_authenticated:
usuario = request.user.id
try:
cliente = Cliente.objects.get(usuario_id = usuario)
bandera = True
except cliente.DoesNotExist:
bandera = False
if bandera == True:
if cliente.id == id_cliente:
viajes = Viaje.objects.filter(cliente_id = id_cliente)
viajes_p = Viaje.objects.filter(cliente_id = id_cliente, estado = 'P')
viajes_c = Viaje.objects.filter(cliente_id = id_cliente, estado = 'C')
viajes_r = Viaje.objects.filter(cliente_id = id_cliente, estado = 'R')
contexto = {
'viajes': viajes,
'viajes_p': viajes_p,
'viajes_c': viajes_c,
'viajes_r': viajes_r,
}
return render(request, 'cliente/servicio.html', contexto)
return HttpResponseRedirect(reverse('home:index'))
These are the models:
from django.db import models
from django.contrib.auth.models import User
from datetime import date, datetime
# Create your models here.
class Cliente(models.Model):
usuario = models.OneToOneField(User, on_delete = models.CASCADE)
cedula = models.CharField(max_length = 10, unique = True)
telefono = models.CharField(max_length = 12)
rol = models.CharField(max_length = 20)
class BitacoraCliente(models.Model):
user = models.ForeignKey(Cliente, on_delete = 'cascade', default = None)
descripcion = models.CharField(max_length = 20)
fecha = models.DateField(default = datetime.today)