django不会从statifiles_dirs加载静态文件 [英] django won't load staticfiles from statifiles_dirs
问题描述
我的 style.css
放置在 appname/static/appname/
中.
我的 settings.py
具有以下代码:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)
然后在我的 base.html
中像这样加载它:
And in my base.html
I load it like this:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">
但是样式没有加载.如果我删除 STATICFILES_DIRS
并将 STATIC_URL ='/static/'
更改为 STATIC_URL ='/static/appname/'
,它可以正常工作,但是我想这不是最佳做法,因为以后我将其他任何应用程序添加到项目中.我可能做错了什么?
But the styles are not loading.
If I remove STATICFILES_DIRS
and change STATIC_URL = '/static/'
to STATIC_URL = '/static/appname/'
, it works perfectly, but I guess it's not the best practice for the case I'll add any other app to the project later. What I might be doing wrong?
推荐答案
只需更改一件事,
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/',
)
您可以从此处直接添加特定的文件名,然后djnago将在该特定目录中搜索.
From here you can directly add the particular file name, and djnago will search in that specific directory.
这篇关于django不会从statifiles_dirs加载静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!