這篇文章主要介紹了如何通過Django使用本地css/js文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
在網上看了很多說Django如何使用本地css/js的文章, 很多都是說的不是很清楚。
今天終于自己來驗證一個能用的了, 記錄下
在manager.py同層級下創建static文件夾, 里面放上css , js, image等文件或者文件夾
我的文件夾tree:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
? FileService git:(master) ? tree . ├── 2kill_port.sh ├── FileService │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ ├── urls.cpython-37.pyc │ │ └── wsgi.cpython-37.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── db.sqlite3 ├── en_bg.jpg ├── fileoperation │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-37.pyc │ │ ├── admin.cpython-37.pyc │ │ ├── models.cpython-37.pyc │ │ └── views.cpython-37.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── __init__.py │ │ └── __pycache__ │ │ └── __init__.cpython-37.pyc │ ├── models.py │ ├── templates │ │ ├── bg_homg.html │ │ └── home.html │ └── views.py ├── files │ ├── 11.log │ ├── 22.log │ ├── 44.log │ ├── th.jpeg │ ├── ?\217?\225快?\205?\ 2020-01-08\ ?\213?\215\2101.50.03.png │ └── ?\235?\231??\212??\224\200?\207??\201.pdf ├── kill_port.sh ├── manage.py └── static ├── images │ └── en_bg.jpg └── style └── style.css |
然后只需在FileService/settings.py中進行設置就行, 在末尾添加以下代碼
1
2
3
4
|
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static' ) ] |
最后只需要在使用的html文件中通過以下方式導入:
1
2
3
4
5
|
< head > < link rel = "stylesheet" type = "text/css" href = "../static/style/style.css" rel = "external nofollow" /> < meta charset = "UTF-8" > < title >文件傳輸</ title > </ head > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/dylancao/p/12170390.html