未找到参数“('',)' 的”category_2“反转

Reverse for 'category_2' with arguments '('',)' not found

提问人:zahra shokrizadeh 提问时间:11/2/2023 最后编辑:zahra shokrizadeh 更新时间:11/5/2023 访问量:94

问:

我希望每当我点击类别时, 我可以看到可用的类别,通过单击每个类别,我可以显示该类别的产品,再次通过单击产品名称,我可以看到该产品的页面。 在这里,单击第一步中的类别将显示此错误。是什么原因

在 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"),

类别.html:

{% extends "auctions/layout.html" %}
{% block body %}
    <h2>All Categories</h2>
    {% for category in categories %}
        <h3><a href="{% url 'category_2' category.pk %}">{{ 
category.name }}</a></h3>
    {% endfor %}
{% endblock %}

product_detail.html(仅相关部分):

<h2>{{ category.name }}</h2>
{% for product in products %}
    <h3>{{ product.name }}</h3>
{% endfor %}

layout.html(在页面顶部显示链接的类别):

{% 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>

错误:

Request Method: GET
Request URL: http://127.0.0.1:8000/category
File "C:\Users\Nazi\Desktop\test\commerce\auctions\views.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']
Python HTML Django 类别

评论

0赞 Ada 11/3/2023
这篇文章很好地总结了您可以尝试的调试步骤:stackoverflow.com/questions/38390177/...
0赞 zahra 11/3/2023
@Ada谢谢。我会试一试。

答:

3赞 willeM_ Van Onsem 11/3/2023 #1

鉴于我理解正确,您希望确定类别的 URL。由于有多个 ,您将需要遍历类别,然后为每个类别使用相应的主键,因此:categoriescategory

{% extends "auctions/layout.html" %}
{% block body %}
  {% for category in categories %}
    <h3><a href="{% url 'category_2' category.pk %}">{{ category.name }}</a></h3>
  {% endfor %}
{% endblock %}

评论

0赞 zahra 11/3/2023
它运行了,但它没有显示任何东西......
0赞 willeM_ Van Onsem 11/3/2023
@zahrashokrizadeh:嗯,数据库中有吗?Category
0赞 zahra 11/3/2023
@ willeM_Van Onsem 是的
0赞 willeM_ Van Onsem 11/3/2023
@zahrashokrizadeh:你有吗?你能用全部/大部分来编辑问题吗?layout{% block body %}layout.html
0赞 zahra 11/3/2023
Onsem虽然工作应该基于与产品相关的表中的数据来完成,但Sql文件中与类别相关的表是空的。