我正在尝试使用“使用Python进行测试驱动开发”一书的在线版本来学习Django。我有一个base.html文件和home.html和list.html文件。现在,CSS适用于home.html,但对于list.html完全没有!他们都从base.html文件中扩展了CSS,那为什么会出现问题呢?以下是文件:
base.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>To-Do lists</title>
<link href="static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="text-center">
<h1>{% block header_text %}{% endblock %}</h1>
<form method="POST" action="{% block form_action %}{% endblock %}">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item"/>
{% csrf_token %}
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
{% block table %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
home.html的:
{% extends 'base.html' %}
{% block header_text %}Start a new To-Do list{% endblock %}
{% block form_action %}/lists/new{% endblock %}
list.html:
{% extends 'base.html' %}
{% block header_text %}Your To-Do list{% endblock %}
{% block form_action %}/lists/{{ list.id }}/add_item{% endblock %}
{% block table %}
<table id="id_list_table">
{% for item in list.item_set.all %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
{% endblock %}
这可能是愚蠢的,但无论如何,谢谢你!
答案 0 :(得分:2)
而不是使用硬编码 CSS链接
<link href="static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
使用Django模板标签:
如果list.html正在呈现yoursite.com/list/
<link href="../static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
if youiteite.com/something/list/然后使用;
<link href="../../static/bootstrap/css/bootstrap.min.css" rel="stylesheet">