我有一个_layout.html
模板如下:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
{% block linkcss %}{% endblock %}
<title>{% block title %}{% endblock %}</title>
<script type="text/javascript" src="{% static 'scripts/jquery-2.1.3.min.js' %}"></script>
{% block scripts %}{% endblock %}
</head>
<body>
{% block head %}{% endblock %}
<table class="page_content">
<tr>
<td>
<div id="content">
{% block content %}{% endblock %}
</div>
</td>
</tr>
</table>
</body>
</html>
该页面home.html
通过以下内容扩展了上述内容:
{% extends "generic/_layout.html" %}
{% load staticfiles %}
{% block title %}{{ cust_title }}{% endblock %}
{% block linkcss %}<link rel="stylesheet" type="text/css" href="{% static '{{ cust_stylesheet }}' %}" />{% endblock %}
{% block scripts %}
<script type="text/javascript">
</script>
{% endblock %}
{% block head %}
<table class="head">
<tr>
<td class="center">
<img src="{% static '{{ cust_header }}' %}">
</td>
</tr>
</table>
{% endblock %}
{% block content %}
<table class="content">
<thead align="center">
<tr>
<th colspan="3" style="text-align: center">{{ cust_message }}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{% endblock %}
该视图是通用的,因为代码检查路径并获取上下文数据和页面......这是一个示例:
# customer configurations constants:
CUSTOMER_CONFIGS = {
'samplewebpage': {
'context': {
'cust_title': "Sample Customer Page",
'cust_stylesheet': "SampleWeb/style.css",
'cust_header': "SampleWeb/sample_header.png",
'cust_message': "Welcome to Sample Web Page"
},
'home': "SampleWebPage/home.html"
},
}
# generic view:
def index(request):
path = request.path.replace("/", "")
context = CUSTOMER_CONFIGS[path]['context']
page = CUSTOMER_CONFIGS[path]['home']
return render(request, page, context)
目录结构:
cust_title
工作正常。那么如何以相同的方式传递cust_stylesheet
位置和cust_header
图像源呢?
实际渲染类似于以下内容:
<link rel="stylesheet" type="text/css" href="/static/%7B%7B%20cust_stylesheet%20%7D%7D" />
<img src="/static/%7B%7B%20cust_header%20%7D%7D">