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 配置
来源:https://www.cnblogs.com/richerdyoung/p/8464188.html