简体   繁体   English

href静态文件Django STATIC_URL和STATICFILES_DIRS

[英]href static files Django STATIC_URL and STATICFILES_DIRS

My settings.py 我的settings.py

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, "static") 
STATIC_URL = '/static/'

In my html page is href to my files using 在我的html页面中,使用

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/default.css">
<img src="{{ STATIC_URL }}images/pythonlogo.jpeg">  

I also tried: 我也尝试过:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/default.css' %}">
<img src="{% static 'images/pythonlogo.jpeg' %}">  

The error I am getting in developer tools is 我在开发人员工具中遇到的错误是

GET http://127.0.0.1:8000/static/css/default.css 404 (NOT FOUND) 
GET http://127.0.0.1:8000/static/images/pythonlogo.jpeg 404 (NOT FOUND)

I tried to print the path on to the web page by simply placing {{STATIC_URL}} on the page and /static/ appears. 我试图通过仅在页面上放置{{STATIC_URL}}并在/static/ {{STATIC_URL}}路径打印到网页上。

My project directory path is: 我的项目目录路径是:

django_test/
    admin/
    article/          <-- app
        templates/
    django_test/
        templates/
    static/
        css/
        images/

STATIC_ROOT is directory where all your static files will be copied by collectstatic command. STATIC_ROOT是将通过collectstatic命令复制所有静态文件的目录。 You should specify your path to STATICFILES_DIRS tuple to use it with built in webserver. 您应指定STATICFILES_DIRS元组的路径,以将其用于内置的Web服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM