Django2.2.1使用CSS
Django的静态文件目录
Django的静态文件目录默认存储于“项目路径/当前app/static/当前app名称/”路径下
注意:这个app必须在settings 中的INSTALLED_APPS里
此时,可以看到settings.py文件中有如下代码
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
CSS文件
CSS文件如下:
h1{
background-color:#667788;
}
td{
color:blue;
}
html文件
<html>
<head>
<title>Contact us</title>
<!--这里的{% load static %}用于加载settings中的静态文件路径,
href="{% static "polls/contact_form.css" %}"用于说明具体引用的静态文件-->
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "polls/contact_form.css" %}"/>
</head>
<body>
<h1>Contact us</h1>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="" method="post">
<!--{% csrf_token %}-->
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
如果修改了CSS却没有生效
可以重新
python manage.py runserver
或者清理浏览器缓存