我已经检查了很多其他线程无法使用Django中的静态文件应用程序提供静态内容,但尚未找到解决方案.
settings.py
STATIC_ROOT = '/opt/django/webtools/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"/home/html/static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
Run Code Online (Sandbox Code Playgroud)
模板
相关线......
<img src="{{ STATIC_URL }}macmonster/img/macmonster-logo-blue.png" >
Run Code Online (Sandbox Code Playgroud)
日志
从日志看起来路径是正确的,但唉它仍然导致404 ..
[10/Feb/2013 16:19:50] "GET /static/macmonster/img/macmonster-logo-blue.png HTTP/1.1" 404 1817
[10/Feb/2013 16:19:51] "GET /static/macmonster/img/macmonster-logo-blue.png HTTP/1.1" 404 1817
Run Code Online (Sandbox Code Playgroud)
Hen*_*son 12
对于静态文件的本地提供,如果你没有设置任何形式的静态文件收集,如果你正在运行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 files
STATICFILES_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',
)
Run Code Online (Sandbox Code Playgroud)
请注意,我已经离开了STATIC_ROOT这里.这是因为我"还没有"静态文件集合的需要.
收集静态文件是为了提供(拼写)服务多个不同静态文件夹文件夹的问题,因此它们合并了staticfiles正常用于帮助解决此问题的应用程序.这样做,以及在文档中描述的是,从您的所有应用程序中获取所有静态文件,并将它们放入一(1)个文件夹,以便在将您的应用程序投入生产时更容易服务.
所以你的问题是你已经"错过"了这一步,这就是为什么你在尝试访问它时获得404的原因. 因此,您需要使用静态文件的绝对路径,即.在mac或unix系统上它应该看起来像这样:
'/Users/cupcake/Documents/Workspaces/myDjangoProject/someOtherFolderPerhapsIfYouWant/static',
Run Code Online (Sandbox Code Playgroud)
此外,您可以简化并"修复"使用硬编码路径的需要,就像我用于说明的那样,并且这样做
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = (
PROJECT_ROOT + '/static/'
)
Run Code Online (Sandbox Code Playgroud)
这也将解决可移植性问题.关于好后#1发现这里
我希望我说得更清楚,否则请纠正我,如果我错了^ _ ^!
有关收集以及如何在较新版本的Django中管理静态文件,请阅读此链接 staticfiles应用程序
| 归档时间: |
|
| 查看次数: |
10989 次 |
| 最近记录: |