Django1.11下调用CSS文件

来源:互联网 发布:单网络计划施工计划图 编辑:程序博客网 时间:2024/06/10 12:48

代码环境:Django 1.11 (Python 3.6.1)


代码文件结构:

-- my

   |-- myblog

       |-- templates

           |-- myblog

               |-- index.html

       |-- __init__.py

       |-- admin.py

       |-- apps.py

       |-- models.py

       |-- tests.py

       |-- urls.py

       |-- views.py

   |-- my

       |-- __init__.py

       |-- setting.py

       |-- urls.py

       |-- wsgi.py

   |-- static

       |-- css

           |-- buttons.css

   |-- db.sqlite3

   |-- manage.py


1. 在settings.py代码末尾添加

STATIC_URL = '/static/'STATICFILES_DIRS = (     os.path.join(BASE_DIR, 'static').replace('\\', '/'),)

2. 根目录下创建文件夹static,再在static下建文件夹css,将需要调用的css文件拷入。


3. 在index.html中<head>部分内添加

{% load staticfiles %}<link href="{% static 'css/buttons.css' %}" type="text/css" rel="stylesheet">
其中 {% load staticfiles %} 是Django中读取静态文件必须添加的部分。


4. 调用成功,直接在部件中添加“id”或“class”。