首页 > 解决方案 > django 不会从 statifiles_dirs 加载静态文件

问题描述

style.css的被​​放置在appname/static/appname/.

settings.py有这个代码:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static/"),
    )

在我的base.html我加载它是这样的:

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">

但是样式没有加载。如果我删除STATICFILES_DIRS并更改STATIC_URL = '/static/'STATIC_URL = '/static/appname/',它会完美运行,但我想这不是我稍后将任何其他应用程序添加到项目中的最佳实践。我可能做错了什么?

标签: pythoncssdjangostylesdjango-staticfiles

解决方案


Just change one thing,

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

It will search in static folder inside your app. Also if you want to add a specific directory,

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), '/your specific directory/',
)

From here you can directly add the particular file name, and djnago will search in that specific directory.


推荐阅读