(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
时,我可以部署我的应用程序: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/ 中出现了错误,我猜测这是因为静态文件没有被发送并且被禁用了。
这样对吗?我不知道...
但是,令人感到奇怪的是,在我的Django管理页面 https://neurorehabilitation.herokuapp.com/admin/ 中,我可以在管理页面中详细查看模板、CSS样式和JS。然后,我不确定Heroku是否正在使用静态文件。
在我的静态文件中发生了什么?
无论如何,我按照我的流程继续进行,在这个机会上,我可以远程部署与
manage.py
命令相关的操作。我进行了检查,因为我可以执行我的应用程序的迁移:
(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)
此外,我可以在部署的应用程序中使用本地工具包创建超级用户...
(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应用程序中的目录结构,我可以详细说明在Heroku中命名为/app
的根项目。
(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项目时设置的结构目录。 我定义了以下内容:
然后,根据先前的情况...
当我将部署到Heroku时,为什么git会搜索路径neurorehabilitation/settings/static
,而这并没有从原始项目源设置,这意味着在部署到Heroku之前,我的电脑上的项目?
我该怎么办?
当我执行heroku run python manage.py collectstatic
命令时,Heroku平台尝试搜索一个不存在或未在我的项目目录结构中设置的目录?
即使我尝试通过heroku run bash
创建/app/neurorehabilitation/settings/static
文件夹,但这并不起作用。
(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部署没有创建类似于本地项目的结构目录?
有人知道原因吗?
在上传到平台时,通过“git push heroku master”,是否可能是因为我还没有完全理解Heroku在部署时与结构目录和文件的工作方式?
非常感谢任何支持。
感谢您的指导 :)
heroku run
远程运行命令;本地所在的目录或本地的 manage.py 副本位于哪里都没有影响。 - Daniel Rosemanheroku run bash
来检查这个问题,在我更新的问题中也展示了这个情况。如果您能给我一些指导,我将非常感激您。:-) - bgarcialBASE_DIR
指令上,它与我的目录结构不匹配。我正在尝试对我的进程和文件进行响应。 - bgarcial