django的靜態文件的引入
1.路徑配置
- 在templates文件夾的同級目錄下新建static文件夾
- 在setting里面寫上STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
2.靜態文件的引入
- 硬編碼
-
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <link rel="stylesheet" href="/static/css/crm/index.css"> 7 </head> 8 <body> 9 <form action=""> 10 <p>測試模板頁面</p> 11 </form> 12 </body> 13 </html>
在html的頭部引入外部靜態文件
- 模板標簽
1 {% load static %} 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Title</title> 7 {# <link rel="stylesheet" href="/static/css/crm/index.css">#} 8 <link rel="stylesheet" href="{% static 'css/crm/index.css' %}"> 9 </head> 10 <body> 11 <form action=""> 12 <p>測試模板頁面</p> 13 </form> 14 </body> 15 </html>
在模板的頭部寫上{% load static %},再在link標簽里寫入{% static "靜態文件的路徑" %}