Search code examples
pythondjangourl-pattern

Django url path matching not working as expected


I have problem with Django model get_absolute_url reverse methods. I have a url pattern that works perfectly but the problem for example

when I visit example.com/blog/python/first-post the path works perfectly, but when I try a random path like, example.com/blog/python-randomr9353/first-post it still works correctly even though it shouldn't because, python-randomr9353 is not a valid path and it should return a page not found error.

Here is my code.

Models

class ArticleSeries(models.Model):
    title = models.CharField(max_length=200)
    series_slug = AutoSlugField(populate_from='title')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:article_list', kwargs={'series_slug': self.series_slug})


class Tag(models.Model):
    title = models.CharField(max_length=50)

    def __str__(self):
        return self.title


class Article(models.Model):
    title = models.CharField(max_length=200)
    article_slug = AutoSlugField(populate_from='title')
    tag = models.ManyToManyField(Tag, default=1, verbose_name='Tag')
    series = models.ForeignKey(ArticleSeries, default=1, verbose_name='Series', on_delete=models.SET_DEFAULT)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:article_detail', args=(self.series.series_slug, self.article_slug))

url patterns

app_name = 'blog'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index_view'),
    path('blog', views.BlogView.as_view(), name='blog_view'),
    path('blog/<slug:series_slug>', views.ArticleListView.as_view(), name='article_list'),
    path('blog/<slug:series_slug>/<slug:article_slug>', views.ArticleDetailView.as_view(), name='article_detail'),
]

Views

class IndexView(TemplateView):
    template_name = 'blog/index.html'
    extra_context = {}


class BlogView(ListView):
    model = ArticleSeries
    template_name = 'blog/blog_view.html'
    context_object_name = 'series_list'

    def get_queryset(self):
        series = ArticleSeries.objects.all()
        return get_list_or_404(series)


class ArticleListView(ListView):
    model = Article
    template_name = 'blog/article_list.html'
    context_object_name = 'article_list'

    def get_queryset(self):
        slug = self.kwargs['series_slug']
        articles = Article.objects.filter(series__series_slug=slug)
        return get_list_or_404(articles)


class ArticleDetailView(DetailView):
    model = Article
    template_name = 'blog/article_detail.html'
    context_object_name = 'article'
    slug_field = 'article_slug'
    slug_url_kwarg = 'article_slug'

    def get_object(self, queryset=None):
        slug = self.kwargs.get('article_slug')
        return get_object_or_404(Article, article_slug=slug)


Solution

  • Try using path('blog/python/<slug:article_slug>', views.ArticleDetailView.as_view(), name='article_detail') in the urls.py instead of path('blog/<slug:series_slug>/<slug:article_slug>', views.ArticleDetailView.as_view(), name='article_detail').

    EDIT:
    At the point where you obtain the value of series_slug, validate this there itself.

    ...
    validSeries = ['python', 'c', 'cpp', 'js'] # etc
    if series_slug not in validSeries:
        # Raise a page not found error