git Heroku 和 Django:“OSError:没有这样的文件或目录:'/app/{myappname}/static'”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19323513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:11:08  来源:igfitidea点击:

Heroku & Django: "OSError: No such file or directory: '/app/{myappname}/static'"

pythondjangogitherokudjango-staticfiles

提问by RexE

I have a Django app on Heroku. I am having some problems with static files (they are loading in one Heroku environmentbut not another), so I tried the debug command recommended here.

我在 Heroku 上有一个 Django 应用程序。我在处理静态文件时遇到了一些问题(它们在一个 Heroku环境中加载,而不是在另一个环境中加载),所以我尝试了此处推荐的调试命令。

$ heroku run python manage.py collectstatic --noinput
Running `python manage.py collectstatic --noinput` attached to terminal... up, run.8771
OSError: [Errno 2] No such file or directory: '/app/{myappname}/static'

Here is my settings.py, which is the same thing Heroku recommends:

这是我的 settings.py,这与 Heroku 推荐的内容相同:

import os
import os.path

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

I get the error whether or not I actually have a directory "static" at the root level in my Git repo (tested it both ways).

无论我的 Git 存储库的根级别是否真的有一个“静态”目录,我都会收到错误消息(两种方式都进行了测试)。

Any ideas?

有任何想法吗?

回答by joerick

It's looking for a folder named 'static' that's next to the settings.py, i.e. in the project folder, not at the root of the git repo.

它正在寻找一个名为“static”的文件夹,该文件夹位于 settings.py 旁边,即在项目文件夹中,而不是在 git 存储库的根目录中。

git root/
git root/{app name}
git root/{app name}/settings.py
git root/{app name}/static/         <- this is what you're missing

Note that empty folders aren't tracked by git, so you'll have to put a blank file in there if it's empty. Alternatively, remove the STATICFILES_DIRSsetting until you need it.

请注意,git 不会跟踪空文件夹,因此如果它是空的,则必须在其中放置一个空白文件。或者,删除STATICFILES_DIRS设置直到您需要它。

回答by Michelle Glauser

I just had this same problem, and here's the solution that worked for me:

我刚刚遇到了同样的问题,这是对我有用的解决方案:

I changed:

我变了:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

to:

到:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'myappfolder/static'),
)

回答by Dima

@joerick's answer above is the thing. However, if you do not want to place another 'static' folder (git root/{your app}/static), you might consider changing the BASE_DIR variable that is initially supplied by django-admin makeproject:

@joerick 上面的回答就是这样。但是,如果您不想放置另一个“静态”文件夹(git root/{your app}/static),您可以考虑更改最初由 django-admin makeproject 提供的 BASE_DIR 变量:

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

which is just the (git root/) directory

这只是 (git root/) 目录