The following excerpts are from a previous book I wrote: It's Django
Use static files
Static files will also be needed in dynamic websites, such as images, css, js files, etc. used by the website. How do we manage and use these static files? First of all, the upper layer mysite底下增加兩個資料夾 static 和 assets,static 資料夾是開發時用來放置靜態檔的目錄,該目錄底下可以新增數個子目錄來放置不同種類的靜態檔,比如說設置 img 來放置圖片或是 css 來放置 css 檔。而 assets is the directory where the static files are placed when the website is actually online. The reason why these two directories are separated is because we need to hand over the management of static files to the web server when it goes online.
A total of three parameters are set here. The detailed description is as follows:
Parameters
Instructions
STATIC_URL
The URL pattern of the static file, here we set it to the path in /static/ ,那麼在網頁路徑中以 /static/ 開始的便會被視為靜態檔,如: http://127.0.0.1/static/hello.png 、 http://127.0.0.1/static/hi.js ,但如果是 http://127.0.0.1/restaurants/static/ 則會匹配urls.py instead of the static file
STATICFILES_DIRS
The folder where static files are placed during development. Allows setting up of multiple folders to indicate the location of static files. As set up above, we can create a folder with BASE_DIR所指示的資料夾底下新增一個 static and put image files, css, and js in it
STATIC_ROOT
Place the folder for static files when going online. After using python manage.py collectstatic ,Django 會將 STAIC_DIRS 下發現的靜態檔複製至 STATIC_ROOT 下。由於當設定檔的 DEBUG 設為 False, Django will not process the return of static files by default. These files are collected into a folder through commands to facilitate web server management and reading
After the above parameters are set, they can be used in the template STATICFILES_DIRS 資料夾下的靜態檔案。假設網站要新增一張圖片 logo.png (放在 mysite/static/img/logo.png ) On the page, we will write this on the template:
...
<img src='/static/img/logo.png'>
...
And you’re done. But if one day the web page path of the static file is forced to be changed to /static_file/ , you have to find the path and modify it one template at a time. Isn't this tiring? A comparison with "Django" is as follows:
Please remember to load staticfiles ,再使用標籤 {% static ...%} 。經過以上調整之後,Django在處理靜態檔的時候便會將 {% static 'img/logo.png' %} 轉為 '/static/img/logo.png' ,而且當有需要修改靜態檔的路徑時,可以放心大膽地修改 settings.py 檔中的 STATIC_URL in the template first, so you don’t need to worry about modifying a large number of templates.
If you refer to the URL corresponding to the View, you can read this document https://docs.djangoproject.com/en/1.9/intro/tutorial03/#removing-hardcoded-urls-in-templates
If you are referring to static files, such as js, css, you can achieve this by changing the STATIC_URL in the Django configuration https://docs.djangoproject.com/en/1.9/howto/static-files/
.NET has outbound urlrewrite It sounds confusing To put it simply, it is to add a filter before the html output Replace all link addresses starting with /
The following excerpts are from a previous book I wrote: It's Django
Use static files
Static files will also be needed in dynamic websites, such as images, css, js files, etc. used by the website. How do we manage and use these static files? First of all, the upper layer
mysite
底下增加兩個資料夾static
和assets
,static
資料夾是開發時用來放置靜態檔的目錄,該目錄底下可以新增數個子目錄來放置不同種類的靜態檔,比如說設置img
來放置圖片或是css
來放置 css 檔。而assets
is the directory where the static files are placed when the website is actually online. The reason why these two directories are separated is because we need to hand over the management of static files to the web server when it goes online.Next, let’s set it up in
settings.py
:A total of three parameters are set here. The detailed description is as follows:
/static/
,那麼在網頁路徑中以/static/
開始的便會被視為靜態檔,如: http://127.0.0.1/static/hello.png 、 http://127.0.0.1/static/hi.js ,但如果是 http://127.0.0.1/restaurants/static/ 則會匹配urls.py
instead of the static fileBASE_DIR
所指示的資料夾底下新增一個static
and put image files, css, and js in itpython manage.py collectstatic
,Django 會將STAIC_DIRS
下發現的靜態檔複製至STATIC_ROOT
下。由於當設定檔的DEBUG
設為False
, Django will not process the return of static files by default. These files are collected into a folder through commands to facilitate web server management and readingAfter the above parameters are set, they can be used in the template
STATICFILES_DIRS
資料夾下的靜態檔案。假設網站要新增一張圖片logo.png
(放在mysite/static/img/logo.png
) On the page, we will write this on the template:And you’re done. But if one day the web page path of the static file is forced to be changed to
/static_file/
, you have to find the path and modify it one template at a time. Isn't this tiring? A comparison with "Django" is as follows:Please remember to load
staticfiles
,再使用標籤{% static ...%}
。經過以上調整之後,Django在處理靜態檔的時候便會將{% static 'img/logo.png' %}
轉為'/static/img/logo.png'
,而且當有需要修改靜態檔的路徑時,可以放心大膽地修改settings.py
檔中的STATIC_URL
in the template first, so you don’t need to worry about modifying a large number of templates.Questions I answered: Python-QA
If you refer to the URL corresponding to the View, you can read this document
https://docs.djangoproject.com/en/1.9/intro/tutorial03/#removing-hardcoded-urls-in-templates
If you are referring to static files, such as js, css, you can achieve this by changing the STATIC_URL in the Django configuration
https://docs.djangoproject.com/en/1.9/howto/static-files/
.NET has outbound urlrewrite
It sounds confusing
To put it simply, it is to add a filter before the html output
Replace all link addresses starting with /
Apache seems to be able to use the .htaccess file to rewrite the accessed address, a regular rule