无法访问 django url(找不到页面)
Can't access django url (page not found)
我有这样的模型:
class Region(models.Model):
region_parent = models.ForeignKey(
"self", blank=True, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
title = models.CharField(max_length=255)
description = RichTextField()
description_on_list = RichTextField(blank=True)
thumbnail = models.ImageField(
upload_to="thumbnail-region", blank=True, max_length=255)
sidebar = RichTextField(blank=True)
ad_manager = models.TextField(blank=True)
meta_keywords = models.TextField(blank=True)
logo_on_navbar = models.ImageField(
upload_to="logo-on-navbar/", blank=True, max_length=255)
display_on_navbar = models.BooleanField(default=True)
slug = models.SlugField(unique=True)
def get_absolute_url(self):
if self.region_parent is not None:
return reverse('vineyards:region', kwargs={'parent': self.region_parent.slug, 'region': self.slug})
else:
return reverse('vineyards:region-without-parent', kwargs={'region': self.slug})
class Vineyard(models.Model):
name = models.CharField(max_length=255)
text = RichTextField()
rating = models.FloatField()
custom_overlay = models.ImageField(
upload_to="custom-rating/", blank=True, max_length=255)
google_map = models.TextField()
wine_rg_url = models.URLField(blank=True)
wine_rg = models.CharField(max_length=255)
wines_url = models.URLField(blank=True)
wines = models.CharField(max_length=255)
size = models.CharField(max_length=255)
grapes = models.CharField(max_length=255)
owner_url = models.URLField(blank=True)
owner = models.CharField(max_length=255)
visits = models.CharField(max_length=255)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
regions = models.ManyToManyField(
Region, blank=True, related_name="regions")
cover = models.ImageField(upload_to="vineyard/", max_length=255)
sidebar = RichTextField(blank=True)
ad_manager = models.TextField(blank=True)
meta_keywords = models.TextField(blank=True)
top_slider = models.BooleanField(default=False)
cover_slider = models.BooleanField(default=False)
hide_rating = models.BooleanField(default=False)
slug = models.SlugField(unique=True)
def get_absolute_url(self):
if self.region.region_parent is not None:
return reverse('vineyards:detail', kwargs={'parent': self.region.region_parent.slug, 'region': self.region.slug, 'slug': self.slug})
else:
return reverse('vineyards:detail-without-parent', kwargs={'region': self.region.slug, 'slug': self.slug})
这是我的 urls.py:
app_name = 'vineyards'
urlpatterns = [
path('<str:parent>/<str:region>/<slug:slug>/form/',
rr_form, name="detail-form"),
path('<str:region>/<slug:slug>/form/',
rr_form, name="detail-without-parent-form"),
path('<str:parent>/<str:region>/', vineyard_region, name="region"),
path('<str:region>/', vineyard_region, name="region-without-parent"),
path('<str:parent>/<str:region>/<slug:slug>/',
vineyard_detail, name="detail"),
path('<str:region>/<slug:slug>/',
vineyard_detail, name="detail-without-parent"),
]
问题是,我无法访问最后 2 个路径(vineyards:detail,vineyards:detail-without-parent)。我认为问题出在 url 的顺序上。我尝试了不同的顺序,但结果总是只有一个或一些 url 在工作,而不是全部。
是否可以有一个 url 和这样的相同路径: slug/slug/ ?或者我应该将 url 分成两个不同的文件吗?有什么建议吗?
谢谢。
您的 url 路径必须是唯一的。否则 Django 应该如何知道请求使用哪个路径?
如果多个路径匹配 url 模式,Django 将使用第一个匹配的路径。
在您的路径中使用独特的 url 模式。例如,
#this is just an example, adjust as it makes sense by your app logic
path('parent/<str:parent>/<str:region>/<slug:slug>/',
vineyard_detail, name="detail"),
path('detail/<str:region>/<slug:slug>/',
vineyard_detail, name="detail-without-parent"),
我有这样的模型:
class Region(models.Model):
region_parent = models.ForeignKey(
"self", blank=True, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
title = models.CharField(max_length=255)
description = RichTextField()
description_on_list = RichTextField(blank=True)
thumbnail = models.ImageField(
upload_to="thumbnail-region", blank=True, max_length=255)
sidebar = RichTextField(blank=True)
ad_manager = models.TextField(blank=True)
meta_keywords = models.TextField(blank=True)
logo_on_navbar = models.ImageField(
upload_to="logo-on-navbar/", blank=True, max_length=255)
display_on_navbar = models.BooleanField(default=True)
slug = models.SlugField(unique=True)
def get_absolute_url(self):
if self.region_parent is not None:
return reverse('vineyards:region', kwargs={'parent': self.region_parent.slug, 'region': self.slug})
else:
return reverse('vineyards:region-without-parent', kwargs={'region': self.slug})
class Vineyard(models.Model):
name = models.CharField(max_length=255)
text = RichTextField()
rating = models.FloatField()
custom_overlay = models.ImageField(
upload_to="custom-rating/", blank=True, max_length=255)
google_map = models.TextField()
wine_rg_url = models.URLField(blank=True)
wine_rg = models.CharField(max_length=255)
wines_url = models.URLField(blank=True)
wines = models.CharField(max_length=255)
size = models.CharField(max_length=255)
grapes = models.CharField(max_length=255)
owner_url = models.URLField(blank=True)
owner = models.CharField(max_length=255)
visits = models.CharField(max_length=255)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
regions = models.ManyToManyField(
Region, blank=True, related_name="regions")
cover = models.ImageField(upload_to="vineyard/", max_length=255)
sidebar = RichTextField(blank=True)
ad_manager = models.TextField(blank=True)
meta_keywords = models.TextField(blank=True)
top_slider = models.BooleanField(default=False)
cover_slider = models.BooleanField(default=False)
hide_rating = models.BooleanField(default=False)
slug = models.SlugField(unique=True)
def get_absolute_url(self):
if self.region.region_parent is not None:
return reverse('vineyards:detail', kwargs={'parent': self.region.region_parent.slug, 'region': self.region.slug, 'slug': self.slug})
else:
return reverse('vineyards:detail-without-parent', kwargs={'region': self.region.slug, 'slug': self.slug})
这是我的 urls.py:
app_name = 'vineyards'
urlpatterns = [
path('<str:parent>/<str:region>/<slug:slug>/form/',
rr_form, name="detail-form"),
path('<str:region>/<slug:slug>/form/',
rr_form, name="detail-without-parent-form"),
path('<str:parent>/<str:region>/', vineyard_region, name="region"),
path('<str:region>/', vineyard_region, name="region-without-parent"),
path('<str:parent>/<str:region>/<slug:slug>/',
vineyard_detail, name="detail"),
path('<str:region>/<slug:slug>/',
vineyard_detail, name="detail-without-parent"),
]
问题是,我无法访问最后 2 个路径(vineyards:detail,vineyards:detail-without-parent)。我认为问题出在 url 的顺序上。我尝试了不同的顺序,但结果总是只有一个或一些 url 在工作,而不是全部。
是否可以有一个 url 和这样的相同路径: slug/slug/ ?或者我应该将 url 分成两个不同的文件吗?有什么建议吗?
谢谢。
您的 url 路径必须是唯一的。否则 Django 应该如何知道请求使用哪个路径?
如果多个路径匹配 url 模式,Django 将使用第一个匹配的路径。
在您的路径中使用独特的 url 模式。例如,
#this is just an example, adjust as it makes sense by your app logic
path('parent/<str:parent>/<str:region>/<slug:slug>/',
vineyard_detail, name="detail"),
path('detail/<str:region>/<slug:slug>/',
vineyard_detail, name="detail-without-parent"),