gpt4 book ai didi

python - 将静态 Assets 从本地机器部署到 heroku - 无法打开文件 'manage.py' : [Errno 2] No such file or directory

转载 作者:太空狗 更新时间:2023-10-29 14:17:54 27 4
gpt4 key购买 nike

我正在尝试将我的 Assets 文件部署到 heroku,我在命令行界面中得到了这个输出:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python manage.py collectstatic
Running python manage.py collectstatic on neurorehabilitation.... up, run.5168
python: can't open file 'manage.py': [Errno 2] No such file or directory
(nrb_dev) ➜ neurorehabilitation_projects git:(master) ✗

这对我来说很奇怪,因为我目前在 manage.py 所在的目录/文件夹中。文件位于

为了尽早部署应用程序,我选择禁用环境变量 DISABLE_COLLECT_STATIC这样:
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DISABLE_COLLECTSTATIC=1
Setting config vars and restarting neurorehabilitation... done
DISABLE_COLLECTSTATIC: 1
(nrb_dev) ➜ neurorehabilitation_projects git:(master)

根据以上,当我执行 git push heroku master我可以部署 mi 应用程序:
remote: 
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 46M
remote: -----> Launching...
remote: Released v15
remote: https://neurorehabilitation.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/neurorehabilitation.git
* [new branch] master -> master
(nrb_dev) ➜ neurorehabilitation_projects git:(master) ✗

根据之前的操作,在我的主链接 https://neurorehabilitation.herokuapp.com/有一个错误,我猜是因为静态文件 des 没有被发送并被禁用。

这是正确的吗?我不知道 ...

但是,在我的 Django Admin https://neurorehabilitation.herokuapp.com/admin/ 中,似乎有些奇怪。我可以在 admin 中详细说明模板和 css 样式和 js,然后,我不知道 heroku 是否正在处理静态文件。

我的静态文件在这种情况下发生了什么?

但是,我遵循我的流程,借此机会我可以远程部署到与 manage.py 相关的 heroku 操作。命令。

我已经检查过这个,因为我可以执行我的应用程序的迁移:
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python  manage.py migrate        
Running python manage.py migrate on neurorehabilitation.... up, run.7836
Operations to perform:
Apply all migrations: contenttypes, auth, userprofiles, sessions, medical_encounter_information, admin
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying userprofiles.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying userprofiles.0002_auto_20160225_2130... OK
Applying userprofiles.0003_auto_20160225_2130... OK
Applying medical_encounter_information.0001_initial... OK
Applying medical_encounter_information.0002_auto_20160225_2130... OK
Applying medical_encounter_information.0003_auto_20160225_2130... OK
Applying medical_encounter_information.0004_auto_20160225_2211... OK
Applying medical_encounter_information.0005_auto_20160225_2211... OK
Applying medical_encounter_information.0006_auto_20160225_2303... OK
Applying medical_encounter_information.0007_auto_20160229_2204... OK
Applying medical_encounter_information.0008_auto_20160229_2208... OK
Applying medical_encounter_information.0009_auto_20160301_0130... OK
Applying medical_encounter_information.0010_auto_20160301_0312... OK
Applying medical_encounter_information.0011_auto_20160301_1525... OK
Applying medical_encounter_information.0012_auto_20160301_1601... OK
Applying medical_encounter_information.0013_auto_20160301_1606... OK
Applying medical_encounter_information.0014_auto_20160301_1629... OK
Applying medical_encounter_information.0015_auto_20160301_1633... OK
Applying medical_encounter_information.0016_auto_20160301_1636... OK
Applying sessions.0001_initial... OK
Applying userprofiles.0004_auto_20160225_2211... OK
Applying userprofiles.0005_auto_20160225_2211... OK
Applying userprofiles.0006_auto_20160225_2303... OK
Applying userprofiles.0007_auto_20160229_2204... OK
Applying userprofiles.0008_auto_20160229_2208... OK
Applying userprofiles.0009_auto_20160301_0130... OK
Applying userprofiles.0010_auto_20160301_0312... OK
Applying userprofiles.0011_auto_20160301_1525... OK
Applying userprofiles.0012_auto_20160301_1601... OK
Applying userprofiles.0013_auto_20160301_1606... OK
Applying userprofiles.0014_auto_20160301_1629... OK
Applying userprofiles.0015_auto_20160301_1633... OK
Applying userprofiles.0016_auto_20160301_1636... OK
(nrb_dev) ➜ neurorehabilitation_projects git:(master)

另外,我可以在我部署的应用程序中从我的本地工具集创建一个 super 用户......
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python  manage.py createsuperuser
Running python manage.py createsuperuser on neurorehabilitation.... up, run.5381

Username: bgarcial
Email address: bgarcial@sample.com
Password:
Password (again):
Superuser created successfully.
(nrb_dev) ➜ neurorehabilitation_projects git:(master) ✗

之后,我之前禁用了环境变量设置 DISABLE_COLLECTSTATIC=1 ( 我已经从我的设置仪表板 heroku 应用程序中删除了环境变量 )

但是,我再次执行 heroku run python manage.py collectstatic命令,我收到此通知:
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python  manage.py collectstatic
Running python manage.py collectstatic on neurorehabilitation.... up, run.9342

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
collected = self.collect()
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
for path in utils.get_files(storage, ignore_patterns):
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/files/storage.py", line 299, in listdir
for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/neurorehabilitation/settings/static'
(nrb_dev) ➜ neurorehabilitation_projects git:(master) ✗

检查这个不方便,我想我的错误在最后:
directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/files/storage.py", line 299, in listdir
for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/neurorehabilitation/settings/static'

而且,这告诉我,在我部署的 heroku 应用程序中不存在 static目录

我已执行 heroku run bash检查我的 heroku 应用程序中的结构目录,我可以详细说明名为 /app 的根项目在 Heroku
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run bash    
Running bash on neurorehabilitation.... up, run.5840
~ $ pwd
/app
~ $

我列出了 /app内容,我以类似的方式查看我的所有目录和文件,就像我在开发本地环境 django 项目中看到的一样
~ $ ls
avatars custom_storages.py django-multiselectfield-0.1.3.tar.gz functional_tests manage.py medical_encounter_information neurorehabilitation Procfile requirements requirements.txt runtime.txt userprofiles

我的错误与这个主题有关:

FileNotFoundError: [Errno 2] 没有这样的文件或目录:“/app/neurorehabilitation/settings/static”

然后我进入到这些目录 /app/neurorehabilitation/settings/但我可以看到不存在名为 static 的目录在里面,这就是 heroku 尝试搜索的方式:
~ $ cd /app/neurorehabilitation/settings/
~/neurorehabilitation/settings $ ls
base.py development.py __init__.py production.py staging.py testing.py
~/neurorehabilitation/settings $

这是因为根据我在本地开发环境中创建 django 项目时设置的结构目录。
我定义了以下内容:

enter image description here

那么,按照之前的情况……

为什么在我执行部署到 heroku 时使用 git,该过程正在搜索到 neurorehabilitation/settings/static 的路径当这不是从原始 sproject 源设置时,这意味着我的机器中的项目在部署到 heroku 之前?

我可以对此采取什么行动?
Heroku 平台,当我执行 heroku run python manage.py collectstatic这个命令尝试搜索一个目录 taht 不存在或不在我的项目的结构目录中设置?

即使我尝试创建 /app/neurorehabilitation/settings/static文件夹通过 heroku run bash ,但这不起作用
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run bash
Running bash on test-nrb.... up, run.1604
~ $ cd neurorehabilitation/settings/
~/neurorehabilitation/settings $ mkdir static
~/neurorehabilitation/settings $ ls -all
total 36
drwx------ 3 u51683 dyno 4096 Apr 1 15:07 .
drwx------ 5 u51683 dyno 4096 Apr 1 15:02 ..
-rw------- 1 u51683 dyno 8106 Apr 1 15:02 base.py
-rw------- 1 u51683 dyno 684 Apr 1 15:02 development.py
-rw------- 1 u51683 dyno 0 Apr 1 15:02 __init__.py
-rw------- 1 u51683 dyno 930 Apr 1 15:02 production.py
-rw------- 1 u51683 dyno 530 Apr 1 15:02 staging.py
drwx------ 2 u51683 dyno 4096 Apr 1 15:07 static
-rw------- 1 u51683 dyno 488 Apr 1 15:02 testing.py
~/neurorehabilitation/settings $ exit
exit
(nrb_dev) ➜ neurorehabilitation_projects git:(master) ✗ heroku run python manage.py collectstatic
Running python manage.py collectstatic on test-nrb.... up, run.5358

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
collected = self.collect()
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
for path in utils.get_files(storage, ignore_patterns):
File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/files/storage.py", line 299, in listdir
for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/neurorehabilitation/settings/static'
(nrb_dev) ➜ neurorehabilitation_projects git:(master)

我不知道如何解决这个问题,为什么在我的 heroku 部署中没有创建类似于我的本地项目的结构目录?

有人是什么原因呢?

可能目前我仍然不明白如何在部署时与结构目录和文件相关的 heroku 工作,当这些通过 git push heroku master 上传到平台时?

任何支持将不胜感激。

感谢您的指导:)

最佳答案

你有没有正确定义:

STATICFILES_DIRS

在您的 settings.py 内?

您是否在本地创建了它并将其添加到 git (作为空目录...)。您拥有的跟踪表明此文件夹丢失。我创建了一个具有以下设置的简单项目:
~/Software/h/hrku $ tail hrku/settings.py 
USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = '/var/www/dj'
STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__), '..', 'static'),)

如您所见 STATICFILES_DIRS只包含一个目录,该目录不存在:
~/Software/h/hrku $ ls -l /home/ozn/Software/h/hrku/static
ls: cannot access /home/ozn/Software/h/hrku/static: No such file or directory

命令 collectstatic失败:
~/Software/h/hrku $ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

/var/www/dj

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
collected = self.collect()
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
for path in utils.get_files(storage, ignore_patterns):
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
directories, files = storage.listdir(location)
File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/files/storage.py", line 299, in listdir
for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/home/ozn/Software/h/hrku/static'

我希望这将引导您解决您的问题。

关于python - 将静态 Assets 从本地机器部署到 heroku - 无法打开文件 'manage.py' : [Errno 2] No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36272067/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com