【python3】 django2.0 加載css 、js 、img 等靜態文件


python : 3.6.4

django : 2.0

 

1 文件目錄  /var/www/mysite

 

2 nginx配置:

server {
    listen 8000; #暴露給外部訪問的端口
    server_name localhost;
        charset utf-8;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8997; #外部訪問8000就轉發到內部8997
    }
    location /static { 
        alias /var/www/mysite/static; 
    }
    
}

3 setting.py 配置

STATIC_URL = '/static/'

STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, "static"), 
    os.path.join(BASE_DIR, "recommend", "static"), 
] 

4 html 配置

{% load static %}
<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <link rel="stylesheet" href="{% static '/css/index.css' %}">

 

 5 重啟uwsgi 

cd /var/www/mysite
killall -9 uwsgi
uwsgi -x mysite.xml

 

注意一點:

debug 開啟時。  django 會幫你加載靜態文件

debug 關閉之后,    django 就無法幫你加載靜態文件了,你要用 nignx 配置

 


免責聲明!

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



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