栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Django Static Files结果为404

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django Static Files结果为404

对于本地提供的静态文件, 如果您尚未 设置任何形式的静态文件收集并且运行的是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应用



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/638863.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号