I trying to add slugs to my service page and my project page, but whenever I try to run my project page I get the Page not found (404) No service found matching the query Request Method: GET Request URL: http://127.0.0.1:8000/project/ Raised by: pages.views.<class 'pages.views.ServiceDetail'>
Here's my class-based code
models.py
class Service(models.Model):
title = models.CharField(max_length=50)
photo = models.ImageField(upload_to='photos/%Y/%m/%d/')
alt = models.CharField(max_length=60, blank=True)
icon = models.CharField(max_length=20)
description = RichTextField()
shortdesc = models.CharField(max_length=255)
slug = models.SlugField(null=False, unique=True)
created_date = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('service_detail', kwargs={'slug': self.slug})
class Project(models.Model):
title = models.CharField(max_length=50)
category = models.CharField(max_length=50)
photo = models.ImageField(upload_to='photos/%Y/%m/%d/')
alt = models.CharField(max_length=60, blank=True)
client = models.CharField(max_length=50)
launched = models.CharField(max_length=50)
demands = models.CharField(max_length=50)
description = RichTextField()
shortdesc = models.CharField(max_length=255)
slug = models.SlugField(null=False, unique=True)
video_link = models.URLField(max_length=100)
created_date = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('project_detail', kwargs={'slug': self.slug})
urls.py
path('<slug:slug>/', views.ServiceDetail.as_view(), name='service_detail'),
path('project/<slug:slug>/', views.ProjectDetail.as_view(), name='project_detail'),
views.py
def project(request):
return render(request, 'project/project.html')
class ProjectDetail (generic.DetailView):
model = Project
template_name = 'project/project_detail.html'
def service(request):
return render(request, 'pages/service.html')
class ServiceDetail (generic.DetailView):
model = Service
template_name = 'pages/service_detail.html'
how can I re-route so that my project page can work out? any help would be grateful
CodePudding user response:
You need to change the order of paths:
path('project/<slug:slug> ....
path('<slug:slug> ....
With you current order, 'project' is interpreted as slug, because paths are matched one after the other.
CodePudding user response:
Your url path in urls.py is pointing to path('project/slug:slug/',...), however you are requesting a page without the /slug:slug/ part being passed http://127.0.0.1:8000/project/ (slug part missing). Request http://127.0.0.1:8000/project/slug-name (replace slug-name with a valid slug) and see if it works.
Also see Razensteins answer
