简体   繁体   English

如何在Django基本模板中呈现查询集?

[英]How I do render a queryset in a Django base template?

I have a base template which extends to almost every other templates in my project. 我有一个基本模板,可扩展到项目中几乎所有其他模板。 I am trying to populate my categories navigation bar with some queryset from my database but am failing with my approach. 我正在尝试使用数据库中的一些查询集填充类别导航栏,但是我的方法失败了。 Since the base template has no url, I am finding it difficult to render. 由于基本模板没有网址,因此我发现很难渲染。 This is what I've tried: 这是我尝试过的:

this is my views function 这是我的意见功能

def base(request):
    categories = list(Category.objects.all())
    context = {
        'categories': categories
    }
    return render(request, 'ads/base.html', context)

this is my html 这是我的HTML

{% for category in categories %}
    <div class="nav-item dropdown">
        <a href="#" class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            { category.name }}&nbsp;
            <span class="float-right text-muted">
                <small>
                    <i class="fas fa-chevron-down"></i>
                </small>
            </span>
        </a>
    </div>
{% endfor %}

I just want to populate my navbar with the categories queryset 我只想用类别queryset填充导航栏

Even if the base view had a URL, that would not make any difference. 即使base视图具有URL,也不会有任何区别。 A URL triggers a view, and that view renders a response. URL触发一个视图,该视图呈现响应。 A template has not (much) to do with it. 模板与它没有太多关系。 A template is sometimes used as a way to generate such response. 有时将模板用作生成此类响应的方法。 But it can only render with data that is passed somehow. 但是它只能使用以某种方式传递的数据进行渲染。 For example through that view, or through template tags, context processors, etc. 例如,通过该视图,或通过模板标签,上下文处理器等。

We can for example define a context processor . 例如,我们可以定义一个上下文处理器 In your app, you can define a function in the context_processors.py file: 在您的应用中,您可以在context_processors.py文件中定义一个函数:

# app/context_processors.py

def categories(request):
    from app.models import Category
    return {'categories': Category.objects.all()}

In the settings.py you can then add the function to the context processors: 然后,您可以在settings.py将该函数添加到上下文处理器中:

# settings.py

# ...

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'app.context_processors.categories'
            ],
        },
    },
]

Now each time you render a template, you will add a categories variable to the context that is mapped on the Category.objects.all() queryset. 现在,每次渲染一个模板时,将添加一个categories变量是在映射上下文Category.objects.all()查询集。 QuerySet s are evaluated lazily, so in case you do not need to iterate over it, the query is not performed on the database. QuerySet是延迟计算的,因此,如果您不需要对其进行迭代,则不会在数据库上执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM