Why is Django trying to open a URL that I don't tell it to?

Viewed 29

Hello Community!

I am creating a small blog with Django, in which I have a single application. It happens that I have already defined a large part of the blog, this is:

  • The Home view.
  • Views for the categories of each publication.
  • View for each of the posts
  • Among other

Now that I have wanted to add the "About Author" view, when it should redirect to its respective HTML template, Django ends up redirecting itself to another template, which generates a NoReverseMatch error.

Simplifying the code, this is:

views.py:

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post, Author, Category

class Home(ListView):
    def get(self, request, *args, **kwargs):
        context = {
            'post': Post.objects.get(title='NamePost')
        }
        return render(request, 'PageWebApp/home.html', context)

class PostSimple(DetailView):
    def get(self, request, slug_post, *args, **kwargs)
    
        context = {
            'post': Post.objects.filter(slug_post=slug_post)
            }

        return render(request, 'PageWebApp/page-simple.html', context)

class PostsCategory(DetailView):
    def get(self, request, category, *args, **kwargs):
        # View that shows each of the categories within the blog
        context = {
            'categories': Category.objects.get(category=category)
        }

        return render(request, 'PageWebApp/posts-category.html', context)

class AboutAuthor(DetailView):
    def get(self, request, slug_autor, *args, **kwargs):

        context = {
            'author': Author.objects.get(slug_author=slug_author)
        } 

        return render(request, 'PageWebApp/page-author.html', context)

urls.py

from django.contrib import admin
from django.urls import path
from PageWebApp import views

urlpatterns = [
     path ('', views.Home.as_view (), name = 'home'),
     # [Here are the URLs to the other project templates (they work fine)]
     # Next the conflictive ones:
     path ('posts- <category> /', views.PostsCategory.as_view (), name = 'posts-category'),
     path ('<slug: slug_post> /', views.PostSimple.as_view (), name = 'page-simple'),
     path ('about-<slug: slug_author> /', views.AboutAuthor.as_view (), name = 'page-author'),
]

I have a template called "base.html" that all the others inherit.

In the Start template, called "home.html" we can achieve, among other things, the following:

<! - HERE GO OTHER TAGS THAT REDIRECT TO OTHER URLS ->
<h4> <a href="{% url 'posts-category' categories.category %}"> See posts from {{categories.category}} </a> </h4>
<h4> <a href="{% url 'page-simple' post.slug_post %}> {{post.title}} </a> </h4>
<h4> <a href="{% url 'page-author' author.slug_author %}> By: {{author.name}} </a> </h4>

As I mentioned earlier, when entering the home window "home.html" I have a series of "a" tags that redirect to various templates, but specifically when I select to go to the URL of the page-author.html template, for some reason Django interprets that it should redirect to page-category and it gives me the error described.

Reverse for 'posts-category' with arguments '('',)' not found. 1 pattern(s) tried: ['posts\\-(?P<category>[^/]+)/$']

I have thoroughly reviewed each HTML template and they are all correctly redirected to the corresponding URLs.

Thanks in advance for your responses and comments.

1 Answers

First of all is it right that there are spaces in your paths? I don't know if that can break anything.

Anyways I think it has to do with the way your paths are set up. This path is catching every path path ('<slug: slug_post> /'. This way all the paths underneath it are first caught by that path because they match that pattern. What you can do in your example is change the order like this:

path ('posts- <category> /', views.PostsCategory.as_view (), name = 'posts-category'),
path ('about-<slug: slug_author> /', views.AboutAuthor.as_view (), name = 'page-author'),
path ('<slug: slug_post> /', views.PostSimple.as_view (), name = 'page-simple'),

This will probably work but the cleanest way to do it is to differentiate the paths based on a / like:

path ('posts/<category>/category/', views.PostsCategory.as_view (), name = 'posts-category'),
path ('posts/<slug: slug_post>/', views.PostSimple.as_view (), name = 'page-simple'),
path ('authors/<slug: slug_author>/', views.AboutAuthor.as_view (), name = 'page-author'),
Related