Home > Net >  Can't access django url (page not found)
Can't access django url (page not found)

Time:12-01

I have models like this :

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})

And this is my 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"),
]

The problem is, I can't access the last 2 paths (vineyards: detail, vineyards: detail-without-parent). I think the problem is in the order of the urls. I have tried different order but the result is always only one or some urls are working, not all.

Is it possible to have a url with the same path like this: slug/slug/ ? Or should I split the url into two different files? Any suggestions?

Thanks.

CodePudding user response:

Your url paths must be unique. How should Django otherwise know which path to use for the request?

If several paths match the url pattern, Django will use the first matching path.

Use unique url patterns in your paths. For example,

#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"),
  • Related