首页 > 解决方案 > Django没有在模板中显示查询

问题描述

我正在尝试在我的 django 模板中显示过滤后的对象,但是由于某种原因,我不知道 django 不会显示它们。

我有一个类似的视图函数和模板,它确实显示,但这个特定的函数没有。

这是我的 urls.py、html 模板和 views.py 文件:

负责 url 的 views.py 函数:

def game_category_list(request, slug):
    template = 'game_category_list.html'
    category = get_object_or_404(gameIdentifier, game_choice=slug)
    post = gameIdentifier.objects.filter(game_choice=category)

    context = {
        'category':category,
        'post':post,
    }

    return render(request, template, context)

Url.py(其中包含另一个名为的 url,它确实显示了与 slug 相关的所有帖子。

urlpatterns = [
    path('', views.index, name='index'),
    path('categories/<slug:slug>', views.game_category_list, name='game_category_list'),
    path('<slug:slug>/', views.category_detail, name='game_category'),

]

这是 game_category_list 函数的 html 文件:

{% extends 'base_layout.html' %}

{% block content %}
<div class='gamecategories'>
    {% for game in post %}
    <div class='gamecategoriesdisplay'>
        <h1>{{game.game_name}}</h1>
    </div>
    {% endfor %}
</div>
{% endblock %}

我不知道为什么这不起作用。任何帮助,将不胜感激。

标签: djangodjango-modelsdjango-templates

解决方案


推荐阅读