对于本地提供的静态文件, 如果您尚未 设置任何形式的静态文件收集并且运行的是Django
1.3+,我相信这是您引用
settings.py静态文件时应采用的方式
# Absolute path to the directory static files should be collected to.# Don't put anything in this directory yourself; store your static files# in apps' "static/" subdirectories and in STATICFILES_DIRS.# Example: "/home/media/media.lawrence.com/static/"STATIC_ROOT = ''# URL prefix for static files.# Example: "http://media.lawrence.com/static/"STATIC_URL = '/static/'# Additional locations of static filesSTATICFILES_DIRS = ( # Put strings here, like "/home/html/static" or "C:/www/django/static". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. '/Users/cupcake/documents/Workspaces/myDjangoProject/someOtherFolderPerhapsIfYouWant/static',)# List of finder classes that know how to find static files in# various locations.STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',# 'django.contrib.staticfiles.finders.DefaultStorageFinder',)
注意,我在
STATIC_ROOT这里省略了。这是因为“现在”我不需要静态文件收集。
收集静态文件是为了缓解(拼写)为多个不同的staticfiles文件夹提供服务时遇到的问题,因此他们合并了
staticfiles通常用于解决此问题的应用程序。它所做的工作(在文档中进行了描述)是从所有应用程序中提取所有静态文件,并将它们放在一(1)个文件夹中,以便在将应用程序投入生产时更容易提供。
因此,您的问题是您已经“错过了”这一步骤,这就是为什么尝试访问它们时得到404的原因。 因此,您需要使用静态文件的绝对路径,
即。在Mac或UNIX系统上,它应该看起来像这样:
'/Users/cupcake/documents/Workspaces/myDjangoProject/someOtherFolderPerhapsIfYouWant/static',
另外,您可以简化和“修复”具有像我用于说明的硬编码路径的需求,并这样做
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))STATICFILES_DIRS = ( PROJECT_ROOT + '/static/')
我希望我把它弄清楚一些,否则,如果我错了,请纠正我^ _ ^!
有关在较新版本的Django中收集以及如何管理静态文件的信息,请阅读此链接
staticfiles应用



