问题描述
我正在尝试按照这些来启用静态我的Django项目中的文件。
我有
STATIC_URL ='/ static /'
pre>
在我的settings.py文件中。我有
'django.contrib.staticfiles',
添加到INSTALLED_APPS =(...)
part
我创建了一个文件夹mysite / static / mysite
并将我的文件放在那里。但是,在我运行服务器之后,我无法访问我的文件,位于http://127.0.0.1:8000/static/mysite/style.css
。
我做错了什么?解决方案在
settings.py
包括以下内容:import os
settings_dir = os.path.dirname(__ file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
STATICFILES_DIRS =(
os.path.join(PROJECT_ROOT,'static / mysite /'),
)
在您的
urls.py
在末尾包括这些行:from django.conf.urls.static import静态
from django.contrib。 staticfiles.urls import staticfiles_urlpatterns
urlpatterns + = staticfiles_urlpatterns()
希望它有帮助。 p>
I'm trying to follow these instructions to enable static files in my Django project.I have
STATIC_URL = '/static/'
in my settings.py file. I have
'django.contrib.staticfiles',
added to theINSTALLED_APPS = (...)
partI created a foldermysite/static/mysite
and put my files there. But after I run the server, I cannot access my files athttp://127.0.0.1:8000/static/mysite/style.css
.What I have done wrong?解决方案In
settings.py
include this:import os settings_dir = os.path.dirname(__file__) PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir)) STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, 'static/mysite/'), )
And in your
urls.py
include these lines at the end:from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += staticfiles_urlpatterns()
Hope it helps.
这篇关于Django中没有加载静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!