我看过很多关于 Django 静态文件问题的不同帖子。不幸的是,他们似乎都没有帮助我。我的 django admin css 很好,但是静态文件给我一个 404 错误。这是我的问题的描述:
在 settings.py 中:
STATIC_URL = '/static/'
STATIC_ROOT = '/Users/me/develop/ember/myproj/static/'
在 urls.py 中:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
我的项目目录:
> static
> myproj
> app
> templates
> ember.hbs
> img
> test.jpg
在我的 ember.hbs
中,我引用了 test.jpg
,但出现 404 错误。这很奇怪,因为我实际上可以使用以下方法提取图像:
file:///Users/me/develop/ember/proj/static/myproj/img/test.jpg
我不知道该怎么做才能解决这个问题,我尝试使用此处提到的 STATICFILES_DIRS
方法:https://docs.djangoproject.com/en/1.8/howto/static-files/
但似乎没有任何效果。
提前致谢!
更多信息:Django 1.8 和 Ember-CLI。
我在 static
目录中托管 ember 项目。
我正在运行 python manage.py runserver
并在端口 8000 上运行 django,同时运行 ember s
并在端口 4200 上运行应用程序。我正在使用 CORS -headers ( https://github.com/ottoyiu/django-cors-headers/ ) 以允许跨站点调用开发。
最佳答案
静态文件将在您创建的“应用程序”(在 INSTALLED_APPS
中找到)或在您在 STATICFILES_DIRS
中指定的其他目录中发现。
假设我做到了:
django-admin.py startproject myproject
然后:
cd myproject
./manage.py startapp myapp
mkdir myapp/static
mkdir myproject/static
会给你一个看起来像这样的目录结构
/Users/me/develop/myproject/
> myapp/
> migrations/
> static/
> __init__.py
> admin.py
> models.py
> tests.py
> views.py
> myproject/
> static/
> __init__.py
> settings.py
> urls.py
> wsgi.py
> manage.py
然后在 settings.py 中将“myapp”添加到 INSTALLED_APPS
。
在这个结构中,myapp/static/
文件会被Django自动发现,因为它是一个静态目录的已安装应用。但是,不会自动发现 myproject/static/
文件,因为“myproject”不在 INSTALLED_APPS
中(不应该)。
这是因为默认的 STATICFILES_FINDERS
设置,即:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
]
第一个查找器 AppDirectoriesFinder
扫描所有 INSTALLED_APPS
的目录,并发现它们可能包含的任何“静态”文件夹。
第二个查找器 FileSystemFinder
扫描您在 STATICFILES_DIRS
中指定的额外目录,默认情况下它是一个空列表。
因此,如果您希望发现/Users/me/develop/myproject/myproject/static/中的那些文件(正如您可能所做的那样),您可以将其添加到您的 settings.py 中:
from os import pardir
from os.path import abspath, dirname, join
# Get the root directory of our project
BASE_DIR = abspath(join(dirname(__file__), pardir))
# Add our project static folder for discovery
STATICFILES_DIRS = (
# /Users/me/develop/myproject/myproject/static/
join(BASE_DIR, 'myproject', 'static'),
)
现在,所有这些都与 STATIC_ROOT
分开,这是一个在您运行 ./manage.py collectstatic< 时所有静态文件都被复制的地方
。对于刚刚开始的项目,这通常位于项目根文件夹中:/Users/develop/me/myproject/static/
所以我们会在 settings.py 中添加:
# /Users/me/develop/myproject/static/
STATIC_ROOT = join(BASE_DIR, 'static')
但这只真正用于生产——这是一种将所有静态 Assets 编译到一个位置的方法,然后用您的服务器(例如 NGINX)将/static/指向它。
在开发过程中,您可以完全跳过 collectstatic,让 Django 即时发现和提供您的静态 Assets 。 Django 在 django.contrib.staticfiles.views.serve
中提供了一个“服务” View 来执行此操作。
在你的 urls.py 中试试这个:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.views import serve
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
if settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', serve),
]
在您的示例中,您要求 Django 从 STATIC_ROOT
提供文件,这也很好,但需要您先运行 ./manage.py collectstatic
,并且每次您的静态 Assets 发生变化时——这可能会很麻烦。
关于python - 更多 Django 静态 404 问题 - 与 ember 配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31737416/