Reputation: 59
One of my Django apps, "Pages", has many different classes in the models.py. Each of them have a slug attribute/field of their own. I'm able to load the URL for the first - "company" (first class) but for some reason can't load the other URLs.
I have tried importing the views separately and creating urls paths for each view. When doing this, only the last URLs (third one) would load.
models.py
class Industry(models.Model):
industry = models.CharField(max_length=140, null=True, blank=True, unique=True)
slug = models.SlugField(max_length=40, null=True, blank=True)
def __str__(self):
return self.industry
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.industry)
super(Industry, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('industry_detail', args=[(self.slug)])
class Subindustry(models.Model):
subindustry = models.CharField(max_length=140, null=True, blank=True, unique=True)
industry = models.ForeignKey(
Industry,
on_delete=models.CASCADE,
related_name='ParentIndustry',
)
slug = models.SlugField(max_length=40, null=True, blank=True)
def __str__(self):
return self.subindustry
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.subindustry)
super(Subindustry, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('subindustry_detail', args=[(self.slug)])
class Company(models.Model):
name = models.CharField(max_length=50, blank=False, unique=True, default=(str(id)))
website = models.URLField(max_length=100)
...
slug = models.SlugField(max_length=40, null=True, blank=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(Company, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('company_detail', args=[(self.slug)])
views.py
from django.views.generic import DetailView
from django.urls import reverse_lazy
# Create your views here.
from .models import Company, Industry, Subindustry
class CompanyDetailView(DetailView):
model = Company
template_name = 'company_detail.html'
slug_field = 'slug'
class IndustryDetailView(DetailView):
model = Industry
slug_field = 'slug'
template_name = 'industry_detail.html'
class SubindustryDetailView(DetailView):
model = Subindustry
slug_field = 'slug'
template_name = 'subindustry_detail.html'
urls.py (Pages app)
from django.urls import path
from .views import CompanyDetailView, IndustryDetailView, SubindustryDetailView
urlpatterns = [
path('<slug:slug>/', CompanyDetailView.as_view(), name='company_detail'),
path('<slug:slug>/', IndustryDetailView.as_view(), name='industry_detail'),
path('<slug:slug>/', SubindustryDetailView.as_view(), name='subindustry_detail'),
]
urls.py (Project level)
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/', include('django.contrib.auth.urls')),
path(r'aoa/', include('qa.urls')),
path('business/', include('pages.urls')),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
]
Expected: Seeing the page when entering right url. E.g. if i try loading http://127.0.0.1:8000/business/construction/ it will load (this is an Industry value for example). Currently only company values load, e.g. http://127.0.0.1:8000/business/macrodepot/ Please note that I do have the HTMLs templates for each class and I am able to see them if I only have one of them in the views.py file.
Actual: The below 404 is received when i enter the url for an industry or subindustry. It's as if is looking for the slug only in the companydetailview.
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/business/construction/ Raised by: pages.views.CompanyDetailView No company found matching the query
Upvotes: 2
Views: 1141
Reputation: 479
It's because django evaluates the urls in the order they are placed in urlpatterns. It receives a request to businesses/construction and calls CompanyDetailView which returns a 404. You need to have a different url pattern for each model:
urlpatterns = [
path('company/<slug:slug>/', CompanyDetailView.as_view(), name='company_detail'),
path('industry/<slug:slug>/', IndustryDetailView.as_view(), name='industry_detail'),
path('subindustry/<slug:slug>/', SubindustryDetailView.as_view(), name='subindustry_detail'),
]
Upvotes: 2