I want whenever I click on the category,
I can see the available categories and by clicking on each one I can display the products of that category and again by clicking on the product name, I can see the page of that product.
Here, clicking on the category in the first step will display this error. what’s the reason
in views.py:
def product_detail(request, product_id):
product = get_object_or_404(Product, pk=product_id)
comments = Comment.objects.filter(product=product)
offers = PriceOffer.objects.filter(product=product).order_by('-offer_price')
off_list = [float(product.first_bid)]
maximum = float(product.first_bid)
if request.method == 'POST':
#comment
#offer
context = {
'product': product,
'comments': comments,
'offers': offers,
'off' : off
}
return render(request, 'auctions/product_detail.html', context)
def category(request):
categories = Category.objects.all()
return render(request, 'auctions/category.html', {'categories': categories})
def category_2(request,cat_id):
category = Category.objects.get(id=cat_id)
products = Product.objects.filter(category=category)
return render(request, 'auctions/product_detail.html', {'category': category,
'products': products})
urls.py:
path("product_detail/<int:product_id>/", views.product_detail,
name="product_detail"),
path("category", views.category, name="category"),
path("category_2/<int:cat_id>/", views.category_2, name="category_2"),
category.html:
{% extends "auctions/layout.html" %}
{% block body %}
<h3><a href="{% url 'category_2' cat_id %}">{{ category.name }}</a></h3>
{% endblock %}
product_detail.html(Only the relevant part):
<h2>{{ category.name }}</h2>
{% for product in products %}
<h3>{{ product.name }}</h3>
{% endfor %}
layout.html(Displays the linked category at the top of the page):
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Auctions{% endblock %}</title>
</head>
<body>
<h1>Auctions</h1>
<div>
{% if user.is_authenticated %}
Signed in as <strong>{{ user.username }}</strong>.
{% else %}
Not signed in.
{% endif %}
</div>
<ul class="nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'index'
%}">Active Listings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'category'
%}">Category</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{% url 'logout'
%}">Log Out</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Log In</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'register' %}">Register</a>
</li>
{% endif %}
</ul>
<hr>
{% block body %}
{% endblock %}
</body>
error:
Request Method: GET
Request URL: http://127.0.0.1:8000/category
File "C:UsersNaziDesktoptestcommerceauctionsviews.py",
line 128, in category
return render(request, 'auctions/category.html', {'categories':
categories})
Exception Type: NoReverseMatch at /category
Exception Value: Reverse for 'category_2' with arguments '('',)'
not found. 1 pattern(s) tried: ['category_2/(?P<cat_id>[0-
9]+)/\Z']
2
Answers
The reason you are getting an error when you click on a category is because you are calling the wrong view function. In your
category.html
template, you are using the{% url 'category_2' cat_id %}
tag to generate the URL for the category link. This tag should be used when you are linking to a category detail page, which is what thecategory_2()
view function is for. However, when you click on a category in the first step, you want to be taken to a page that lists all of the products in that category. This is what thecategory()
view function is for.To fix the error, you should change the
{% url 'category_2' cat_id %}
tag in yourcategory.html
template to{% url 'category' cat_id %}
. This will ensure that when you click on a category, you are taken to the correct view function.Here is an example of the corrected
category.html
template:Once you have made this change, your code should work as expected. You will be able to click on a category to see a list of all the products in that category, and you will be able to click on a product name to see the page for that product.
Given I understand it correctly, you want to determine the URL for a category. Since there are multiple
categories
, you will need to loop over the categories, and for eachcategory
then use the corresponding primary key, so: