Django URL dispatcher - Try next view

Alright, let me give you guys an example;

We have the following url configuration in Django.

Django will try to match the url with the rules down below. Once it finds a match, it will use the appropriate view and lookup the object in the model.

The thing is, once it finds a match in the URL pattern, it will match the view. But once the object in the view can't be found, it will return a page not found (404) error.

#urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('articles/<slug:category>/<slug:editor>/', views.articles_by_editor),
    path('articles/<slug:category>/<slug:theme>/', views.articles_by_theme)
]

We have the following url patterns, we can sort articles either by the editor or by theme. We do this to create a logical url structure for SEO purposes.

Is their any way we can redirect to another view once the object isn't found?

Can we modify the dispatch method to return to the url patterns and find the following matching rule?

What about redirection like this:

def articles_by_editor(request, category, editor):
    try:
        article = Article.objects.get(category=category, editor=editor)
        # return article
    except Article.DoesNotExist:
        # redirect to another view
        return redirect('articles_by_theme', category=category)

Alright,

Based on the suggestion from Sunderam Dubey, I'wrote a function view, which uses two differn routes to the same view.

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('articles/<slug:category>/<slug:slug>/', views.article_theme_or_detail_view, name='article_by_theme'),
    path('articles/<slug:category>/<slug:slug>/', views.article_theme_or_detail_view, name='article_detail')

]

views.py

def article_theme_or_detail_view(
    request,
    category_slug,
    slug=None
):
    """
    This view could either be for a theme view or detailview,
    depending on the slug.
    """
    try:
        # Check if the slug represents a theme
        theme = ArticleTheme.objects.get(slug=slug)
        article_list = Article.object.filter(theme=theme)
        
        # Add context
        context = {
            'theme': theme,
            'article_list': article_list
        }

        # Render the template with context
        return render(
            request,
            'article_by_theme.html',
            context
        )
    except ArticleTheme.DoesNotExists:
        # The theme does not exist so the slug must be for a detail view
        context = {
            article = Article.objects.get(slug=slug)
        }

        return render(
            request,
            'article_detail.html',
            context
        )

Todo:

  • Remove one of the url routes
Back to Top