【发布时间】:2021-03-28 17:00:05
【问题描述】:
我不确定它是变量还是标签。
我有两个应用程序(不同的样式,不同的导航栏)但想从 db 打印同一个文档。
我创建了 base.html、带有扩展的模板,一切都运行良好。但是。
模板主体是从数据库中填充的,是html代码的一部分。
在这段代码中有<p>The contact email is: blabla@firstapp.example.com</p>
当然这对 secondapp 来说是错误的。
我尝试使用<p>The contact email is: {{ app_email }}</p> 更改正文,并使用上下文设置它
但它不起作用 - 它会打印
The contact email is: {{ app_email }}
模板/文档.html:
{% extends base %}
{% load crispy_forms_tags %}
{% load static %}
{% block head %}
<title>{{ title }}</title>
{% endblock %}
{% block content %}
<div class="container">
<div class="row justify-content-center">
<div class="col-9">
<h1 class="mt-2">{{ document.header }} !!!</h1>
<hr class="mt-0 mb-4">
{% autoescape off %}
{{ document.body }}
{% endautoescape %}
</div>
</div>
</div>
{% endblock %}
firstapp.views.py:
def myview(request):
context = {
'app_title' : 'First',
'title' : 'First - document',
'app_name' : 'first',
'app_email' : 'contact@first.example.com',
'document' : get_document('document1'),
'base' : 'first/base.html'
secondapp.views.py:
def myview(request):
context = {
'app_title' : 'Second',
'title' : 'Second - document',
'app_name' : 'second',
'app_email' : 'contact@second.example.com',
'document' : get_document('document1'),
'base' : 'second/base.html'
这样可以吗?也许有一些过滤器?
已编辑: 现在我知道,我必须在 get_document 中预渲染它。但是如何传递未知参数呢?
此功能有效 - 但必须添加 sth do 参数 (*args? **kwargs?) 并重新定义 Context{{ *args? **kwargs?? }}
def get_document(name):
doc = Doc.objects.get(name=name)
doc.body = Template(doc.body).render(Context{{'app-email':'contact@first.example.com'}})
return doc
【问题讨论】:
-
是
get_document('document1')结果中的电子邮件行吗?
-
@saquintes 完全正确
标签: django django-templates django-template-filters