[官網靜態文件介紹] https://docs.djangoproject.com/en/1.10/howto/static-files/
# settings.py 配置靜態資源文件
# STATIC_URL別名設置,默認會去STATICFILES_DIRS下找路徑,這里helloworld代指statics
# 好處就是無論后台怎么更改路徑,前面任然只需要調用helloworld即可
STATIC_URL = '/helloworld/'
<script src="/helloworld/jquery-3.2.1.js'"></script> # 利用helloworld映射下面的statics
STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),) # 這里是個數組
----------------------------------------------------------------------------------------------------------------------------------
settigs.py:增加STATICFILES_DIRS靜態資源路徑配置,名稱為創建的文件夾名稱
'DIRS': [os.path.join(BASE_DIR, 'templates')], # 設置templates的路徑為Django以前版本
# 'DIRS': [], # 注釋掉該行,此為Django 2.0.1最新版本
# 'django.middleware.csrf.CsrfViewMiddleware',
...省略默認配置
STATIC_URL = '/static/'
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) # 原配置
# 靜態資源文件
STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),) # 現添加的配置,這里是元組,注意逗號
# 我們一般只用 STATIC_URL,但STATIC_URL會按着你的STATICFILES_DIRS去找
templates/static_index.html
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"></head>
<body>
<p id="h1">數據展示</p>
{# {% load staticfiles %}#} {# 這種靜態加載也是可以的 #}
{% load static %}
<!--第一種方法-->
<script src="{% static '/jquery-3.2.1.js' %}"></script>
{# setting.py里面已經配置了靜態資源加載statics,所以這里直接寫/XXX.js #}
<!--第二種方法【推薦使用,簡單,不需要上面的{% load %}】-->
<script src="/static/jquery-3.2.1.js"></script>
<script>
$("p").css("color","red")
</script>
</body>
</html>
mysite2/urls.py
from django.contrib import admin
from django.urls import path
from blog import views
urlpatterns = [
path(r'static_index/', views.static_index), # 將路徑名跟函數進行映射
]
views.py
from django.shortcuts import render
def static_index(request):
return render(request, "static_index.html")
# 這里第一個參數必須是rquest, 第二個參數是我們頁面HTML的名稱
# 這里直接寫HTML名稱是因為Django將參數封裝再來settings.py文件內
頁面顯示:
簡單問題記錄
問題一: You called this URL via POST, but the URL doesn't end in a slash[斜線]
-->意思是POST請求未結束,少一個斜線
[靜態配置問題解決] https://www.cnblogs.com/Andy963/p/Django.html




