我正在尝试在开发(DEBUG=True
)模式下在Django中提供静态文件。我有一个像这样的目录结构:
my_project/
...
static/
img.png
在我的settings.py
我有这个:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILE_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
在my_project/urls.py
我有这个:
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
# ...
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('app.landing.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
这一切似乎都是正确的。当我访问http://127.0.0.1:8000/static/img.png时,我得到了404。
我做错了什么?
答案 0 :(得分:0)
您可以通过多种方式对其进行调试。这是我的方法。
main settings.py:
DEBUG = False
TDEBUG = True
urls.py:
from django.conf import settings
import os
if settings.TDEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': os.path.join(os.path.dirname(__file__), 'static')} ),
)