Reputation: 1
NoReverseMatch at /category/1/
Reverse for 'category' with arguments '('',)' not found. 1 pattern(s) tried: ['category/(?P<category_id>[0-9]+)/$']
This error is displayed when navigating to a site category from the home page. On googling I found that my url does not match the url in the template and django reports it. But I still don't understand what is wrong, because everything is written to match the urls.py templates.
urls.py(app folder)
from django.urls import path
from .views import *
urlpatterns = [
path('', home_page, name='home_page'),
path('category/<int:category_id>', get_category, name='category'),
path('news/<slug:slug>', view_news, name='news'),
]
urls.py(project folder)
import debug_toolbar
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('news.urls')),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('__debug__/', include(debug_toolbar.urls)),
]
base.html
{% load news_tags %}
{% get_all_categories as categoies %}
{% for category in categoies %}
<li><a href="{% url 'category' category.pk %}" class="nav-link px-2" style="color: rgb(179, 179, 179);">{{ category.title }}</a></li>
{% endfor %}
index.html(home page)
{% for item in page_obj %}
<div class="card mb-5">
<div class="card-header">
<div class="category_name"> <a href="{% url 'category' item.category_id %}" style="text-decoration: none; color: rgb(93, 92, 92);">{{ item.category }}</a> - {{ item.created_at }}</div>
</div>
<a href="/news/{{ item.slug }}" style="text-decoration: none; color: rgb(26, 25, 25);">
<div class="card-body">
<h3 class="card-title">{{ item.title }}</h3>
<p class="card-text">{{ item.content|linebreaks|truncatewords:50 }}</p>
</div>
</a>
</div>
{% endfor %}
views.py
def home_page(request):
all_publicated_blogs = News.objects.filter(is_published=True,)
return render(request, 'news/index.html',
{'page_obj': Pagination(request, all_publicated_blogs).pagination(10),})
def get_category(request, category_id):
news_by_category = News.objects.filter(category_id=category_id, is_published=True)
one_news = news_by_category[0]
return render(request, 'news/category.html',
{'page_obj': Pagination(request, news_by_category).pagination(10), 'news': one_news,})
models.py
from django.db import models
from ckeditor.fields import RichTextField
class News(models.Model):
title = models.CharField(max_length=50,verbose_name='Заголовок',)
content = RichTextField()
created_at = models.DateTimeField(auto_now_add=True,verbose_name='Дата Создания')
updated_at = models.DateTimeField(auto_now=True)
is_published = models.BooleanField(default=False, verbose_name='Опубликовано')
category = models.ForeignKey('Category', on_delete=models.PROTECT, null=True, verbose_name='Категория')
slug = models.SlugField(verbose_name='URL', max_length=40, unique=True,)
def __str__(self):
return self.title
class Meta:
verbose_name = 'Новость'
verbose_name_plural = 'Новости'
ordering = ['-created_at']
class Category(models.Model):
title = models.CharField(max_length=50, db_index=True,
verbose_name='Название категории')
def __str__(self):
return self.title
class Meta:
verbose_name = 'Категорию'
verbose_name_plural = 'Категории'
ordering = ['id']
news_tags.py
from django import template
from news.models import Category
register = template.Library()
@register.simple_tag()
def get_all_categories():
return Category.objects.all()
category.html
{% get_all_categories as categoies %}
{% for category in categoies %}
{% if category == news.category %}
<li><a href="{% url 'category' category_id=category.pk %}" class="nav-link px-2" style="color: white;display: inline; padding: 17px 6px; border-bottom: solid 5px #707990;">{{ category.title }}</a></li>
{% else %}
<li><a href="{% url 'category' category_id=category.pk %}" class="nav-link px-2" style="display: inline; padding: 0px 6px;">{{ category.title }}</a></li>
{% endif %}
{% endfor %}
Upvotes: 0
Views: 73
Reputation: 111
I think on index.html
in url tag the reference to category is off.
Change href="{% url 'category' item.category_id %}"
to href="{% url 'category' item.category.id %}"
Upvotes: 1