一整天我都在为这个问题绞尽脑汁,但无法找出问题所在。它发生在我将我的项目从一台机器复制到另一台机器之后。
设置.py
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
在 INSTALLED_APPS 中也提到了“django.contrib.staticfiles”。
文件夹结构:
Django-Projects (root)
project
app
static
css
home.css
js
manage.py
模板:
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/home.css' %}">
网址.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('app.urls')),
)
它在打开模板时在控制台中抛出错误:
GET http://127.0.0.1:8000/static/css/home.css
Failed to load resource: the server responded with a status of 404 (NOT FOUND)
这里可能出了什么问题?请帮帮我。非常感谢!
最佳答案
在你的 settings.py 中
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'
然后在你的模板文件中:
<link rel="stylesheet" href="{{ STATIC_URL }}css/home.css">
根据您显示的目录根结构,我认为上述设置应该有效。虽然还没有测试过。让我知道它是否有效。
关于python - Django : Static content not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755457/