Please help me, I don't know why I get this error, I'm following a youtube tutorial and I don't understand why the error.
i try changing the path in urls but it didn't work.
I was looking for more solutions to the same problem and none of them work.
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Reserva(models.Model):
auto = models.CharField(max_length=100)
fecha = models.DateTimeField(auto_now_add=True)
fecha_reserva = models.DateField(null=True)
hora1 = '9:00am - 10:00am'
hora2 = '10:00am - 11:00am'
hora3 = '11:00am - 12:00pm'
hora4 = '12:00pm - 13:00pm'
hora5 = '14:00pm - 15:00pm'
hora6 = '15:00pm - 16:00pm'
hora7 = '16:00pm - 17:00pm'
hora8 = '17:00pm - 18:00pm'
hora_reserva_CHOICES = [
(hora1, '9:00am - 10:00am'),
(hora2, '10:00am - 11:00am'),
(hora3, '11:00am - 12:00pm'),
(hora4, '12:00pm - 13:00pm'),
(hora5, '14:00pm - 15:00pm'),
(hora6, '15:00pm - 16:00pm'),
(hora7, '16:00pm - 17:00pm'),
(hora8, '17:00pm - 18:00pm')
]
hora_reserva = models.CharField(
max_length=17,
choices=hora_reserva_CHOICES,
default=hora1
)
Mantención = 'Mantención'
Reparación = 'Reparación'
Limpieza = 'Limpieza'
Servicios_CHOICES = [
(Mantención, 'Mantención'),
(Reparación, 'Reparación'),
(Limpieza, 'Limpieza'),
]
Servicios = models.CharField(
max_length=10,
choices=Servicios_CHOICES,
default=Limpieza,
)
User = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.auto ' -by ' self.User.username
url.py
from django.contrib import admin
from django.urls import path
from Main import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('signup/', views.signup, name='signup'),
path('reserva/', views.reserva, name='reserva'),
path('reserva/crear/', views.crearreserva, name='crearreserva'),
path('reserva/<reserva_id>/',
views.detalle_reserva, name='detalle_reserva'),
path('logout/', views.logouto, name='logout'),
path('login/', views.logino, name='login')
]
views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.contrib.auth import login, logout, authenticate
from .forms import ReservaForm
from .models import Reserva
def home(request):
return render(request, 'Home.html')
def signup(request):
if request.method == 'GET':
return render(request, 'SignUp.html', {
'form': UserCreationForm
})
else:
if request.POST['password1'] == request.POST['password2']:
try:
user = User.objects.create_user(username=request.POST['username'],
password=request.POST['password1'])
user.save()
login(request, user)
return redirect('reserva')
except:
return render(request, 'SignUp.html', {
'form': UserCreationForm,
"error": 'Usuario ya existente'
})
return render(request, 'SignUp.html', {
'form': UserCreationForm,
"error": 'Contraseña no coinside'
})
def reserva(request):
todas = Reserva.objects.filter(User=request.user)
return render(request, 'Reserva.html', {'todas': todas})
def crearreserva(request):
if request.method == 'GET':
return render(request, 'CrearReserva.html', {
'form': ReservaForm
})
else:
try:
form = ReservaForm(request.POST)
nueva_reserva = form.save(commit=False)
nueva_reserva.User = request.user
nueva_reserva.save()
return redirect('reserva')
except ValueError:
return render(request, 'CrearReserva.html', {
'form': ReservaForm,
'error': 'Ingrese datos validos porfavor'
})
def detalle_reserva(request, reserva_id):
reserva = Reserva.objects.get(pk=reserva_id)
return render(request, 'DetalleReserva.html', {
'reserva': reserva
})
def logouto(request):
logout(request)
return redirect('home')
def logino(request):
if request.method == 'GET':
return render(request, 'LogIn.html', {
'form': AuthenticationForm
})
else:
user = authenticate(
request, username=request.POST['username'], password=request.POST['password'])
if user is None:
return render(request, 'LogIn.html', {
'form': AuthenticationForm,
'error': 'Usuario o Contraseña erroneas'
})
else:
login(request, user)
return redirect('reserva')
html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ServiExpress</title>
<link type="text/css" rel="stylesheet" href="{% static '/css/css.css' %}">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.2/assets/css/docs.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<!--Menú-->
<nav >
<div >
<a href="#">
<img src="{% static '/img/logo3.png' %}" alt="logo" width="300px">
</a>
<button type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span ></span>
</button>
<ul >
<!--Home-->
<li >
<a aria-current="page" href="/">Home</a>
</li>
<!--Servicios-->
<li >
<a aria-current="page" href="#">Servicios</a>
</li>
<!--Reserva-->
<li >
<a aria-current="page" href="/reserva">Reserva</a>
</li>
<!--Quienes somos-->
<li >
<a aria-current="page" href="#">¿Quienes somos?</a>
</li>
<!--Contacto-->
<li >
<a aria-current="page" href="#">Contacto</a>
</li>
</ul>
<!--Inicio sesión-->
{% if user.is_authenticated %}
<button type="button" onclick="location.href='/logout'">Cerrar sesión</button>
{% else %}
<button type="button" onclick="location.href= '/login'">Iniciar
sesión</button>
<button type="button" onclick="location.href= '/signup'">Crear
usuario</button>
{% endif %}
</div>
</nav>
<ul>
{% for Reserva in todas %}
<a href="{% url 'detalle_reserva' reserva.id %}">
<li>
<h1>{{Reserva.auto}}</h1>
<p>{{Reserva.Servicios}}</p>
<p>{{Reserva.fecha_reserva}}</p>
<p>{{Reserva.hora_reserva}}</p>
</li>
</a>
{% endfor %}
</ul>
</body>
</html>
Thank you very much for helping me with my stupid problem
CodePudding user response:
You need to be correction for id of Reserva url
like this...
{% for Reserva in todas %}
<a href="{% url 'detalle_reserva' Reserva.id %}">
<li>
<h1>{{Reserva.auto}}</h1>
<p>{{Reserva.Servicios}}</p>
<p>{{Reserva.fecha_reserva}}</p>
<p>{{Reserva.hora_reserva}}</p>
</li>
</a>
{% endfor %}