Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Heroku上的Django--找不到静态文件_Django_Heroku_Django Staticfiles - Fatal编程技术网

Heroku上的Django--找不到静态文件

Heroku上的Django--找不到静态文件,django,heroku,django-staticfiles,Django,Heroku,Django Staticfiles,我在Heroku上运行Django。我可以成功运行collectstatic,但当我访问该站点时,Django显然无法找到我的静态文件。下面是我的设置中的一个片段——我认为它主要是标准的东西: STATIC_ROOT = '' # URL prefix for static files. # Example: "http://media.lawrence.com/static/" STATIC_URL = '/static/' PROJECT_DIR = os.path.abspath(os

我在Heroku上运行Django。我可以成功运行collectstatic,但当我访问该站点时,Django显然无法找到我的静态文件。下面是我的设置中的一个片段——我认为它主要是标准的东西:

STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))


# 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.
    os.path.join(PROJECT_DIR, 'static'),
)

# List of finder classes that know how to find static files in
# various locations.
if CLAYS_ENV == 'dev':
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )

在我的例子中,CLAYS_ENV变量将被设置为“dev”。关于Django为什么可以成功运行collectstatic,但之后却找不到文件,你有什么想法吗?

collectstatic和为静态媒体服务是两码事。Collectstatic只需将您的静态媒体放入您的
static\u目录中

为静态文件提供服务是服务器的一个过程,而不是
collectstatic
命令。我以前从未部署到heroku,但您是否有url将静态资产映射到它们的位置

if not settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )
如果不是settings.DEBUG:
urlpatterns+=模式(“”,
(r'^static/(?P.*)','django.views.static.service',{'document\u root':settings.static\u root}),
)

您使用的是哪台服务器?不过,通常您希望服务器服务于静态媒体,而不是django。在apache中,这是通过设置一个
别名来实现的,建议在使用Heroku时通过CDN(如Amazon S3)提供静态文件。虽然您仍然可以直接从Heroku为这些请求提供服务,但处理动态请求的同一进程也在为静态数据提供服务,这浪费了处理时间。此外,对于媒体文件,必须使用CDN,因为Heroku的文件系统是“ephimeral”:每次部署新代码时,都会从头开始重新创建Cedar堆栈的新映像,并进行新代码签出。在部署之间创建的所有未被Git跟踪的文件都将丢失。

在较新版本的Django上,您可能需要通过在url模式的前面添加“url”来稍微修改上述内容,例如
url(r'^static…
此方法不应在生产环境中使用。相反,您应该直接通过wsgi提供文件。您可以按照以下说明以这种方式提供:摘自文章本身:这对于大多数应用程序来说是完全足够的,但顶级应用程序可能希望探索使用具有Django仓库。