django - 如何在 Django 基本模板中呈现数据库查询集?
问题描述
我希望在我网站的所有页面上显示一些查询集。
从所有迹象来看,合乎逻辑的做法是将标记包含在 base.html 中。
不幸的是,base.html 没有被 url 呈现或调用。所以我很难做到这一点。
我做了什么。1
- 我在调用 reders index.html 的视图中进行了查询。
- 将查询集传递到上下文中。
- 在我的 index.html 模板中扩展 base.html
- 使用模板标记来调用查询集。
它在我当地运作良好。但是在部署到heroku后,它停止显示在我的索引上。
视图.py
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 片段
<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>
我还能如何完成这项工作?
解决方案
推荐阅读
- c# - 附加类型为“ModelName”的实体失败,因为同一类型的另一个实体已经具有相同的主键值
- c++ - 具有三个参数的重载 operator new 和 operator delete
- python - 如何在不执行的情况下检查 python 2 脚本的错误
- python - 在python中获取以下内容的正则表达式
- javascript - 如何在 pugjs 中添加一个多于一个单词的样式属性
- javascript - 修改(前缀或后缀)现有正则表达式以从匹配项中排除尾随句点
- php - 使用 POST 时在刷新时确认表单重新提交行为
- javascript - 如何在网页等社交媒体中加载特定数量的内容?
- svg.js - SVG.JS 使用现有路径向文本添加文本路径
- javascript - 如何在 SIPML5 WebRTC 中配置 REFER 呼叫?