Django中配置静态文件夹static

1、在项目根目录下创建 statics 目录。

2、在 settings 文件的最下方配置添加以下配置:

STATIC_URL = '/static/' # 别名 
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, "statics"), 
]

3、在 statics 目录下创建 css 目录,js 目录,images 目录,plugins 目录, 分别放 css文件,js文件,图片,插件。

4、在模板中使用之前需要加入 {% load static %} 代码,具体使用方式如下:

<!DOCTYPE html>
<html lang="en"> <head> {% load static %} <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="{% static 'libs/bootstrap/css/bootstrap.min.css' %}"> <link rel="stylesheet" href="{% static 'libs/toastr/css/toastr.css' %}"> <link rel="stylesheet" href="{% static 'libs/bootstrap-table-master/bootstrap-table.css' %}"> <script src="{% static 'libs/jquery/jquery-1.11.2.min.js' %}"></script> <script src="{% static 'libs/bootstrap/js/bootstrap.min.js' %}"></script> <script src="{% static 'libs/toastr/js/toastr.min.js' %}"></script> <script src="{% static 'libs/bootstrap-table-master/bootstrap-table.js' %}"></script> <script src="{% static 'libs/bootstrap-table-master/locale/bootstrap-table-zh-CN.js' %}"></script> </head> <body> </body> </html>

猜你喜欢

转载自www.cnblogs.com/samve/p/13171531.html