When I run the app (after already creating the diff categories and sub-categories in the admin page) I try to click on the categories link on the nav-bar and they show up, however, the problem is when i click on the specific category link. Here is when it sends the NoReverseMath error. The following image is an image from my terminal showing the error:
The following is my code:
Thanks in advance!
urls.py (app folder)
from django.urls import path
from . import views
urlpatterns = [
path('', views.home,name="home"),
path('items',views.all_items,name="list_items"),
path('categories',views.category,name="categories"),
path('show_category/<category_id>', views.show_category,name="show_category"),
]
models.py
from django.db import models
from django.urls import reverse
import uuid
# Create your models here.
class ClothingItem(models.Model):
category = models.ForeignKey('Categorie',null=True,on_delete=models.CASCADE)
sub_category = models.ForeignKey('SubCategorie',null=True,on_delete=models.CASCADE)
name = models.CharField( max_length=50)
brand = models.ForeignKey('Brand',null=True,on_delete=models.CASCADE)
description = models.TextField(max_length=1000,blank=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("clothingitem_detail", kwargs={"pk": self.pk})
class Categorie(models.Model):
category = models.CharField(max_length=100)
def __str__(self):
return self.category
class SubCategorie(models.Model):
sub_category = models.CharField('Clothing Type',max_length=100)
def __str__(self):
return self.sub_category
class Brand(models.Model):
brand_name = models.CharField(max_length=100)
class Meta:
ordering = ['brand_name']
def get_absolute_url(self):
return reverse("brand_detail", kwargs={"pk": self.pk})
def __str__(self):
return self.brand_name
views.py
from django.shortcuts import render
from .models import ClothingItem,Categorie,SubCategorie
# Create your views here.
def home(request):
return render(request,'item_type/home.html',{})
def show_category(request, category_id):
category = Categorie.objects.get(id=category_id)
sub_category = SubCategorie.objects.all()
context = {'category':category,'sub_category':sub_category}
return render(request, 'item_type/show_category.html',context)
def all_items(request):
items_list = ClothingItem.objects.all()
return render(request, 'item_type/clothing_list.html',{'items_list':items_list})
def category(request):
category_list = Categorie.objects.all()
return render(request, 'item_type/categories.html',{'category_list':category_list})
base.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Notte e Mattina</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
</head>
<body>
{% include 'item_type/navbar.html' %}
<br/>
<div class="container">
{% block content %}
{% endblock %}
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</body>
</html>
categories.html
{% extends 'item_type/base.html' %}
{% block content %}
<h1>Categories</h1>
{% for i in category_list %}
<a href="{% url 'show_category' category.id %}"> {{i}}</a>
{% endfor %}
{% endblock %}
clothing_list.html
{% extends 'item_type/base.html' %}
{% block content %}
<h1>List of items</h1>
<br/>
{% for i in items_list %}
<div class="card">
<div class="card-header">
{{i}}
</div>
<div class="card-body">
<h5 class="card-title">{{i.sub_option}}</h5>
<p class="card-text"> <strong> Brand: {{i.brand}} </strong>
<br/>{{i.description}}</p>
</div>
</div>
<br/>
<br/>
{% endfor %}
{% endblock %}
home.html
{% extends 'item_type/base.html' %}
{% block content %}
<h1>hello</h1>
{% endblock %}
navbar.html
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="{% url 'home' %}">Notte e Mattina</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="{% url 'categories' %}">Categories</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'list_items' %}">All Items</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
show_category.html (im still working on this specific view and page)
{% extends 'item_type/base.html' %}
{% block content %}
<h1>{{category}}</h1><br/>
{% for i in sub_category %}
{{i}}
{% endfor %}
{% endblock %}