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.