I'm trying to create a simple car dealership app and i'm stuck. I have searched days and i have tried so many times to fix this but i can't. What i'm doing wrong i don't know and my brain is not working anymore. Thanks for help.
Error:
NoReverseMatch at /
Reverse for 'car_detail' with keyword arguments '{'slug': '2021-ram-1500-trx-crew-cab-4x4-57-box'}' not found. 1 pattern(s) tried: ['(?P<id>[0-9] )/(?P<slug>[-a-zA-Z0-9_] )$']
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.2.8
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'car_detail' with keyword arguments '{'slug': '2021-ram-1500-trx-crew-cab-4x4-57-box'}' not found. 1 pattern(s) tried: ['(?P[0-9] )/(?P[-a-zA-Z0-9_] )$']
Exception Location: C:\Users*\Desktop\Testdealer\envdealer\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: C:\Users*\Desktop\Testdealer\envdealer\Scripts\python.exe
Python Version: 3.9.7
Models.py:
from django.db import models
import datetime
from django.urls import reverse
class Car(models.Model):
brand = models.CharField(max_length=100)
CATEGORY = (
('New', 'New'),
('Used', 'Used')
)
slug = models.SlugField(max_length=200, null=True, unique=True)
category = models.CharField(max_length=50, choices=CATEGORY)
image_main = models.ImageField(upload_to='images')
body_style = models.CharField(max_length=100, blank=True)
engine = models.CharField(max_length=100, blank=True)
stock_number = models.IntegerField(blank=True, null=True)
mpg = models.CharField(max_length=100, blank=True)
exterior_color = models.CharField(max_length=100, blank=True)
interior_color = models.CharField(max_length=100, blank=True)
drivetrain = models.CharField(max_length=100, blank=True)
mileage = models.IntegerField(blank=True, null=True)
sold = models.BooleanField(default=False, blank=False)
transmission = models.CharField(max_length=50, blank=True)
YEAR_CHOICES = [(r, r) for r in range(1940, datetime.date.today().year 1)]
year = models.IntegerField(
('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)
power = models.IntegerField()
fuel = models.CharField(max_length=50, blank=True)
price = models.CharField(max_length=10, blank=True)
description = models.TextField()
date = models.DateField()
def __str__(self):
return self.brand
def get_absolute_url(self):
return reverse('car_detail', kwargs={'slug': self.slug})
# def get_absolute_url(self):
# return reverse('car_detail', kwargs={
# 'car_id': self.id
# })
class CarImage(models.Model):
"""
The Product Image table.
"""
name = models.CharField(max_length=200, blank=True)
car = models.ForeignKey(
Car, on_delete=models.CASCADE, related_name="car_image")
image = models.ImageField(
verbose_name="image",
help_text="Upload a product image",
upload_to="images/",
default="images/default.png",
)
alt_text = models.CharField(
verbose_name="Alturnative text",
help_text="Please add alturnative text",
max_length=255,
null=True,
blank=True,
)
class Meta:
verbose_name = "Car Image"
verbose_name_plural = "Car Images"
def __str__(self):
return self.name
Views.py:
from django.shortcuts import render, get_object_or_404
from .models import Car, Dealer, CarImage
from django.core.mail import send_mail
def car_detail(request, id, slug):
cars = Car.objects.get(pk=id)
images = CarImage.objects.filter(car_id=id)
context = {'cars': cars, 'images': images}
return render(request, 'car_detail.html', context)
Urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from cars import views
..........
path('<int:id>/<slug:slug>', views.car_detail, name="car_detail"),
CodePudding user response:
The .get_absolute_url()
[Django-doc] of the Car
model only specifies the slug
, not the id
parameter:
class Car(models.Model):
# ⋮
def get_absolute_url(self):
# add id to the context ↓
return reverse('car_detail', kwargs={'id': self.pk, 'slug': self.slug})