Django中靜態資源配置和使用例項
阿新 • • 發佈:2021-01-16
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
在專案的目錄下,建立static目錄
在模板中使用靜態資源:
1、在html的首行加上 {% load static %}
2、匯入靜態資源:{% static ‘相對於static的路徑’ %}
{% load staticfiles %}
#這2個也可以用
{#<script src="../static/js/jquery-3.5.1.min.js" ></script>#}
{#<script src="../static/js/register.js"></script>#}
#這種比較正宗
<script src="{% static 'js/jquery-3.5.1.min.js' %}"></script>
<script src="{% static 'js/register.js' %}"></script>
1.靜態資源
1.在django專案中放在static目錄的下就是靜態資源 如: css,js,img,檔案,html
2.動態資源
1.一些可以動態執行的程式碼,如: python, template語言下的html
2. template語言下的html其實django的一種特殊的語言
我發現我引用js的時候。得放到html下面,放上面的話,AJAX的請求就發不出去了
例如
<body>
<div style="margin: 15% 40%;">
<h1>歡迎註冊!</h1>
<p>
<label for= "id_username">使用者名稱:</label>
<input type="text" id="id_username" name="username" placeholder="使用者名稱" autofocus required />
</p>
<p>
<label for="id_password">密碼:</label>
<input type="password" id="id_password" placeholder="密碼" name="password" required ><br>
<span id="msg"></span><br>
</p>
<button class="send_Ajax">註冊</button>
</div>
{#<script src="../static/js/jquery-3.5.1.min.js"></script>#}
{#<script src="../static/js/register.js"></script>#}
<script src="{% static 'js/jquery-3.5.1.min.js' %}"></script>
<script src="{% static 'js/register.js' %}"></script>
</body>