Django:找不到静态内容 [英] Django : Static content not found
问题描述
我已经为此整整一整天都无法解决,但无法找出问题所在。在我将项目从一台机器复制到另一台机器后发生了。
I have been breaking my head over this for a full day but can't figure out the problem. It happened after I copied my project from one machine to another.
Settings.py
Settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
在INSTALLED_APPS中也提及了'django.contrib.staticfiles'。
Mentioned 'django.contrib.staticfiles' in INSTALLED_APPS as well.
文件夹结构:
Django-Projects (root)
project
app
static
css
home.css
js
manage.py
模板:
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/home.css' %}">
urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('app.urls')),
)
打开控制台时会在控制台中引发错误模板:
It throws an error in the console on opening the template :
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)
这里可能出什么问题了?请帮帮我。
What might be wrong here ? Please help me out. Much thanks!
推荐答案
在您的settings.py
In your 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">
有了您显示的目录根结构,我认为上面的设置应该起作用。虽然还没有测试。让我知道它是否有效。
With the directory root structure that you have shown, I think the above setting should work. Have not tested it though. Let me know if it works.
这篇关于Django:找不到静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!