如何在Django基模板中呈现数据库查询集?
我希望在我的网站的所有页面上显示一些查询集 从所有迹象来看,合乎逻辑的做法是将标记包含在base.html中 不幸的是,base.html没有被url呈现或调用。所以我很难做到这一点 我所做的一切。 一,如何在Django基模板中呈现数据库查询集?,django,Django,我希望在我的网站的所有页面上显示一些查询集 从所有迹象来看,合乎逻辑的做法是将标记包含在base.html中 不幸的是,base.html没有被url呈现或调用。所以我很难做到这一点 我所做的一切。 一, 我在调用reders index.html的视图中进行了查询 将查询集传递到上下文中 我的index.html模板中的扩展base.html 使用模板标记调用查询集 它在我的本地工作得很好。但在部署到heroku后,它停止显示在我的索引上 view.py from django.shortcu
from django.shortcuts import render, get_object_or_404
from .models import CompanyProfile, CompanyServices
def index_view(request):
company_services = CompanyServices.objects.all()
company_profile = CompanyProfile.objects.all()
context = {
'profile': company_profile,
'services': company_services,
}
return render(request, 'webpages/index.html', context=context)
base.html snippet
<body>
<!-- ======= Top Bar ======= -->
<section id="topbar" class="d-none d-lg-block">
<div class="container d-flex">
<div class="contact-info mr-auto">
<i class="icofont-envelope"></i><a href="mailto:contact@example.com">{{profile.company_email}}</a>
<i class="icofont-phone"></i> {{profile.company_phone1}}
</div>
{{profile.company_phone1}}
我还可以怎样做呢?要将数据加载到
base.html
,最干净的方法是使用上下文处理器。看看上下文处理器方法允许您获取请求
对象,还可以像请求
对象一样返回上下文中的参数。注意:只有在视图中使用render将数据加载到base.html
时,它们才能成功工作,最干净的方法是使用上下文处理器。看看上下文处理器方法允许您获取请求
对象,还可以像请求
对象一样返回上下文中的参数。注意:只有在视图中使用渲染时,它们才能成功工作