django 输入代码后仍无法显示输出

ltskdhd1  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(180)

尽管输入了代码,但此字段中没有显示任何输出。原因是什么?
在views.py:

def category(request, category_id):
    category = get_object_or_404(Category, pk=category_id)
    products = category.product_set.all()
    return render(request, 'category.html', {'category': category, 'products': products})

字符串
在urls.py:

path("category<int:category_id>", views.category, name="category")


在category.html中:

{% extends "auctions/layout.html" %}
{% block body %}

<h2>{{ category.name }}</h2>
<ul>
    {% for product in products %}
        <li>{{ product.category }}</li>
    {% endfor %}
</ul>
{% endblock %}


in layout.html(单击时显示类别。):

{% load static %}

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>{% block title %}Auctions{% endblock %}</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <link href="{% static 'auctions/styles.css' %}" rel="stylesheet">
    </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">
                {% for category in categories %}
                    <a class="nav-link" href="{% url 'category' category.id %}">Category</a>
                {% endfor%}
            </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>
</html>


一个标题应在布局中创建,以显示通过点击它的所有类别。

zz2j4svz

zz2j4svz1#

你的代码在这里有一些错误

path("category<int:category_id>", views.category, name="category")

字符串
试试这个

path("category/<int:category_id>/", views.category, name="category")


希望有帮助。我会尽快设置并给予一次尝试。

相关问题