使用 slugs 和基于 class 的视图时找不到视图
Failing to find views when using slugs and class based views
我的一个 Django 应用程序 "Pages" 在 models.py 中有许多不同的 classes。他们每个人都有自己的鼻涕虫attribute/field。我能够加载第一个 URL - "company"(第一个 class)但由于某种原因无法加载其他 URLs.
我试过单独导入视图并为每个视图创建 urls 路径。执行此操作时,只会加载最后的 URLs(第三个)。
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(页面应用)
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(项目级别)
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'),
]
预期:向右输入时看到页面url。例如。如果我尝试加载 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/ 请注意,我确实有每个 class 的 HTML 模板,如果我在 views.py 文件中只有其中一个模板,我就能看到它们。
实际:当我输入某个行业或子行业的 url 时收到以下 404。好像只在 companydetailview 中寻找 slug。
找不到页面 (404)
请求方式:GET
请求 URL:http://127.0.0.1:8000/business/construction/
提出者:pages.views.CompanyDetailView
找不到符合查询的公司
这是因为 django 按照 url 放在 url 模式中的顺序计算 url。它收到对 businesses/construction 的请求并调用 CompanyDetailView,其中 returns 一个 404。您需要为每个模型设置不同的 url 模式:
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'),
]
我的一个 Django 应用程序 "Pages" 在 models.py 中有许多不同的 classes。他们每个人都有自己的鼻涕虫attribute/field。我能够加载第一个 URL - "company"(第一个 class)但由于某种原因无法加载其他 URLs.
我试过单独导入视图并为每个视图创建 urls 路径。执行此操作时,只会加载最后的 URLs(第三个)。
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(页面应用)
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(项目级别)
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'),
]
预期:向右输入时看到页面url。例如。如果我尝试加载 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/ 请注意,我确实有每个 class 的 HTML 模板,如果我在 views.py 文件中只有其中一个模板,我就能看到它们。
实际:当我输入某个行业或子行业的 url 时收到以下 404。好像只在 companydetailview 中寻找 slug。
找不到页面 (404) 请求方式:GET 请求 URL:http://127.0.0.1:8000/business/construction/ 提出者:pages.views.CompanyDetailView 找不到符合查询的公司
这是因为 django 按照 url 放在 url 模式中的顺序计算 url。它收到对 businesses/construction 的请求并调用 CompanyDetailView,其中 returns 一个 404。您需要为每个模型设置不同的 url 模式:
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'),
]