Python 如何在Django中的footer.html中呈现常见内容
我已经设置了Django模板,其中包括Python 如何在Django中的footer.html中呈现常见内容,python,django,python-3.x,Python,Django,Python 3.x,我已经设置了Django模板,其中包括index.html、header.html和footer.html 在footer.html中,我想动态设置电话号码、电子邮件和地址。我可以看到我可以从views.py通过context传递它,但是footer.html将包含在所有模板中,因此我需要在views.py中的每个函数中传递它,这是不好的 所以我想创建一个公共函数,并从footer.html调用它。这样做对吗?或者如果你有任何其他想法,那么请告诉我怎么做 views.py: footer.htm
index.html
、header.html
和footer.html
在footer.html
中,我想动态设置电话号码、电子邮件和地址。我可以看到我可以从views.py
通过context
传递它,但是footer.html
将包含在所有模板中,因此我需要在views.py中的每个函数中传递它,这是不好的
所以我想创建一个公共函数,并从footer.html
调用它。这样做对吗?或者如果你有任何其他想法,那么请告诉我怎么做
views.py:
footer.html:
电子邮件
{{portal\u contact\u email}
电话
住址
版权所有@
var CurrentYear=新日期().getFullYear()
文件写入(当前年度)
{#作者的主题}
为此,您可以创建一个
根据,ContextProcessors
将请求对象作为其参数,并返回要合并到上下文中的项的字典
您案例中的自定义ContextProcessor可能如下所示:
def页脚数据(请求):
返回{'phone_number':'xyz',…}
然后将其附加到settings.py,以便实际使用:
模板=[
{
...
“选项”:{
“上下文处理器”:[
“myapp.my\u上下文\u处理器.页脚\u数据”,
...
在模板中,只需使用
{{{phone\u numer}}
即可访问这些变量。为此,您可以创建一个
根据,ContextProcessors
将请求对象作为其参数,并返回要合并到上下文中的项的字典
您案例中的自定义ContextProcessor可能如下所示:
def页脚数据(请求):
返回{'phone_number':'xyz',…}
然后将其附加到settings.py,以便实际使用:
模板=[
{
...
“选项”:{
“上下文处理器”:[
“myapp.my\u上下文\u处理器.页脚\u数据”,
...
在模板中,只需使用{{{phone\u numer}}
即可访问这些变量
def index(request):
portal_contact_email = preferences.MyPreferences.portal_contact_email
context = {'portal_contact_email': portal_contact_email,}
return render(request, 'mysite/index.html', context)
<footer class="bg-dark footer-section">
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-4">
<h5 class="text-light">Email</h5>
<p class="text-white paragraph-lg font-secondary">{{ portal_contact_email }} </p>
</div>
<div class="col-md-4">
<h5 class="text-light">Phone</h5>
<p class="text-white paragraph-lg font-secondary"></p>
</div>
<div class="col-md-4">
<h5 class="text-light">Address</h5>
<p class="text-white paragraph-lg font-secondary"></p>
</div>
</div>
</div>
</div>
<div class="border-top text-center border-dark py-5">
<p class="mb-0 text-light">Copyright @<script>
var CurrentYear = new Date().getFullYear()
document.write(CurrentYear)
</script>
{# a theme by <a href="https://themefisher.com">themefisher.com</a>#}
</p>
</div>
</footer>