04 pycharm創建django項目&靜態文件


pycharm創建django項目  

  

  

 

  創建好的目錄結構如下:

  

   可以看到在settings里django自動加了路徑,不需要我們手動再去加模板路徑。

   

 

靜態文件

  靜態文件,不會有變化的文件,比如樣式css文件,js文件,jquery文件,這些不變的文件需要放一個目錄下,瀏覽器在請求的時候請求到指定的目錄就行。

  創建靜態文件目錄,將靜態文件都放進去:

    03 django路由層\django_urls\statics

    

  settings配置靜態文件目錄:STATICFILES_DIRS = [os.path.join(BASE_DIR,'statics'),]   # 指定靜態文件地址,所有的靜態文件都放這個目錄

  

 

 

簡單靜態文件實例

實現h3標簽的點擊事件,本來是紅色,點擊變綠色。

03 django路由層\django_urls\django_urls\urls.py

from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index),
]

 

03 django路由層\django_urls\app01\views.py

from django.shortcuts import render

# Create your views here.

def index(request):
    return  render(request,'index.html')

 

03 django路由層\django_urls\templates\index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
    <link rel="stylesheet" href="/static/app01/my.css">
</head>
<body>
<h3>Hello World!</h3>
</body>
<script type="text/javascript" src="/static/jquery-3.3.1.js"></script>
<script type="text/javascript" src="/static/app01/my.js"></script>
</html>

 

03 django路由層\django_urls\statics\jquery-3.3.1.js

jquery下載路徑:https://way2tutorial.com/jquery/jquery_download.php#jquery_download

 

03 django路由層\django_urls\statics\app01\my.css

h3{
            color: red;
        }

 

03 django路由層\django_urls\statics\app01\my.js

$(function () {
        $('h3').click(function () {
            this.style.color = "green";
        })
});

 

訪問:http://127.0.0.1:8080/index/

  

點擊字體后

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM