从本地将静态文件部署到Heroku - 无法打开文件'manage.py': [Errno 2] 没有那个文件或目录。

3
我正在尝试将我的资产文件部署到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时,我可以部署我的应用程序:
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项目时设置的结构目录。 我定义了以下内容:

enter image description here

然后,根据先前的情况...

当我将部署到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在部署时与结构目录和文件的工作方式?
非常感谢任何支持。
感谢您的指导 :)

2
你误解了正在发生的事情。 heroku run 远程运行命令;本地所在的目录或本地的 manage.py 副本位于哪里都没有影响。 - Daniel Roseman
@DanielRoseman,我一直在重新构思我的问题,目前的情况是Heroku搜索静态文件到一个在我的部署中不存在的目录中。我可以通过heroku run bash来检查这个问题,在我更新的问题中也展示了这个情况。如果您能给我一些指导,我将非常感激您。:-) - bgarcial
1
你可以分享一下你的设置文件吗? - ahmed
@ahmed 感谢您的关注,我的问题出在BASE_DIR指令上,它与我的目录结构不匹配。我正在尝试对我的进程和文件进行响应。 - bgarcial
2个回答

1
你是否正确定义了:


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'

我希望这能帮助您解决问题。

感谢关注,我的问题出在我的 BASE_DIR 指令上,它与我的目录结构不匹配。当然,BASE_DIRSTATICFILES_DIRS 指令有影响。我试图对我的过程和文件进行回应。 - bgarcial

0

我将逐步展示我是如何在Heroku上部署我的Django应用程序的。我希望这个问题对其他人有用,也要感谢那些花时间帮助我的人。

1. 在Heroku中设置我的静态文件设置

  • 将设置配置到Heroku中

关键是能够将我的静态文件部署到Heroku中,在我的settings/base.py中实现,根据ahmedOz123在他们的回答中所说。

特别地,我遇到了一个问题,就是PROJECT_ROOT指令的值。Heroku建议以这种方式使用PROJECT_ROOT指令:

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

我按照Heroku的建议设置了PROJECT_ROOT我在settings/base.py中将其命名为BASE_DIR),但总是出现错误,然后我选择了将BASE_DIR指令保留为Django默认的方式,就像我的PROJECT_ROOT指令与上述Heroku配置相关联一样。

我遇到的错误:

FileNotFoundError: [Errno 2] No such file or directory: '/app/neurorehabilitation/settings/static'

当我将BASE_DIR设置为:

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

最后,我的settings/base.py文件内容如下:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

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

# This line tells Django to look for static files in a folder named static inside each of our apps.
STATIC_URL = '/static/'

作为我的BASE_DIR (Heroku中的PROJECT_ROOT),我定义了文件系统路径,最终我的STATICFILES_DIRS指令也像Oz123在他们的回复中所引用的那样被占用了。 :D

# With this configuration, Django will look for static files in a folder named static inside each app and into the neurorehabilitation/static folder
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

然后根据我的项目中的部署设置(此图中的粉色正方形),我的settings/production.py包含以下内容:

# ------ *** -------------
# For deploy to heroku
# ------ *** -------------

# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

在我的虚拟环境中安装dj-database-url包也是必要的:

(nrb_dev)   neurorehabilitation_projects git:(master)  pip install dj-database-url
Collecting dj-database-url
Installing collected packages: dj-database-url
Successfully installed dj-database-url-0.4.0
You are using pip version 8.1.0, however version 8.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(nrb_dev)   neurorehabilitation_projects git:(master) 

在此项目的requirements/production.txt文件中添加dj-database-url软件包/依赖项,方法如下:
dj-database-url==0.4.0

我的问题在于,当Heroku部署应用程序并尝试将我的资产复制和处理到Amazon S3时,collectstatic命令无法工作,而我的静态文件根据我的设置配置存储在其中。


此外,我想分享我的Heroku部署过程,我认为这将是有用的甚至是轶事。

要将Django应用程序部署到Heroku,必须在项目根目录中拥有以下文件:

  • requirements.txt 文件

我曾经在Django项目的根目录下有myproject/requirements/文件夹,但是Heroku部署过程不是这样工作的。

然后我在与requirements/文件夹相同级别或层次结构中创建了一个包含以下内容的requirements.txt文件:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ cat requirements.txt 
-r requirements/production.txt%                                                 (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗

我的requirements/production.txt文件保持不变:

-r base.txt
gunicorn==19.4.5
dj-database-url==0.4.0

requirements/base.txt文件包含了我项目中所有依赖或必要的软件包。

在部署过程中,我需要Heroku安装3.4版本的Python,因此需要在我的项目根目录下创建runtime.txt文件并指定相应的版本。

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ cat runtime.txt 
python-3.4.3
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗

Procfile 是一个文本文件,位于应用程序的根目录中,它定义了进程类型,并明确声明了启动应用程序所需执行的命令。

我的 Procfile 保持不变:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ cat Procfile 
web: gunicorn neurorehabilitation.wsgi --log-file -
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 

2. 通过Heroku工具包测试从我的计算机访问

需要安装Heroku工具包

它提供了访问Heroku命令行界面的功能,用于管理和扩展应用程序和插件等。

我通过以下方式测试从我的计算机访问heroku:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku login
Enter your Heroku credentials.
Email: botibagl@gmail.com
Password (typing will be hidden): 
Logged in as botibagl@gmail.com
(nrb_dev) ➜

3. 创建我的Heroku应用程序

根据此响应,由于要部署的应用程序是python/Django,因此Heroku应用程序应该以这种方式创建:

(nrb_dev)   neurorehabilitation_projects git:(master)  heroku create test-nrb --buildpack heroku/python
Creating test-nrb... done, stack is cedar-14
Setting buildpack to heroku/python... done
https://test-nrb.herokuapp.com | https://git.heroku.com/test-nrb.git
(nrb_dev)   neurorehabilitation_projects git:(master)  

然后,我的Heroku应用程序就这样在我的Web仪表板中创建了

enter image description here

4. 在我的项目中为 Heroku 设置环境变量

我在本地项目中有一些环境变量用于隐藏我的密钥、数据库用户名、名称和密码,以及访问我的亚马逊 Web 服务和 S3 帐户的某些权限,以及用于生产服务器的设置(DJANGO_SETTINGS_MODULE)。

必须指示 Heroku 知道这些环境变量的方式。

根据此链接,我们将为部署的应用程序配置变量。然后我按照以下方式设置了我的环境变量:

  • DJANGO_SETTINGS_MODULE

我的部署将由 settings/production.py 文件指导,该文件从 settings/base.py 继承,其中包含所有应用程序域包/依赖项。然后我的 Heroku 中的 DJANGO_SETTINGS_MODULE 变量保持不变:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DJANGO_SETTINGS_MODULE=neurorehabilitation.settings.production 
Setting config vars and restarting test-nrb... done
DJANGO_SETTINGS_MODULE: neurorehabilitation.settings.production
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
  • SECRET_KEY(密钥)

我执行:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set SECRET_KEY='mysecretkey'
Setting config vars and restarting test-nrb... done
SECRET_KEY: mysecretkey
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
  • DATABASE_NAME

我执行:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DATABASE_NAME=mydatabasename
Setting config vars and restarting test-nrb... done
DATABASE_NAME: mydatabasename
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
  • DATABASE_USER 数据库用户

我执行:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DATABASE_USER=mydatabaseuser
Setting config vars and restarting test-nrb... done
DATABASE_USER: mydatabaseuser
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
  • DATABASE_PASSWORD : 单引号中的值

我执行:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DATABASE_PASSWORD='mydatabasepassword'
Setting config vars and restarting test-nrb... done
DATABASE_PASSWORD: mydatabasepassword
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
  • Amazon Web Services访问密钥ID:单引号中的值

我执行:

(nrb_dev)   neurorehabilitation_projects git:(master)  heroku config:set AWS_ACCESS_KEY_ID='my_aws_access_key_id'
Setting config vars and restarting test-nrb... done
AWS_ACCESS_KEY_ID: my_aws_access_key_id
(nrb_dev)   neurorehabilitation_projects git:(master) 
  • Amazon Web Services Secret Access Key:单引号中的值

我执行:

(nrb_dev)   neurorehabilitation_projects git:(master)  heroku config:set AWS_SECRET_ACCESS_KEY='my_aws_secret_access_key'
Setting config vars and restarting test-nrb... done
AWS_SECRET_ACCESS_KEY: my_aws_secret_access_key
(nrb_dev)   neurorehabilitation_projects git:(master)  
  • Amazon Web Services 存储桶名称:在单引号中的值

我执行:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set AWS_STORAGE_BUCKET_NAME='my-bucket-name-in-s3' 
Setting config vars and restarting test-nrb... done
AWS_STORAGE_BUCKET_NAME: my-bucket-name-in-s3
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 

然后根据我的 Web 仪表板上面的选项,环境变量包括:

enter image description here

5. 执行commit操作到Heroku仓库

首先,我们检查要提交的文件

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   requirements.txt
    new file:   runtime.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   neurorehabilitation/settings/base.py
    modified:   neurorehabilitation/settings/development.py
    modified:   neurorehabilitation/settings/production.py
    modified:   requirements.txt
    modified:   runtime.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .idea/
    Procfile
    gunicorn_start

(nrb_dev) ➜  neurorehabilitation_projects git:(master)

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add requirements.txt 
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add runtime.txt     
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add Procfile 
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add neurorehabilitation/settings/base.py 
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add neurorehabilitation/settings/development.py
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add neurorehabilitation/settings/production.py

进行提交操作commit

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git commit -m 'Setup changes and configurations for deploy to heroku'
[master 69d69fe] Setup changes and configurations for deploy to heroku
 6 files changed, 38 insertions(+), 5 deletions(-)
 create mode 100644 Procfile
 create mode 100644 requirements.txt
 create mode 100644 runtime.txt
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗

6. 将我们的代码应用程序部署到Heroku存储库

在此之前,我检查了我的远程存储库。我已经引用了heroku别名。

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git remote -v
heroku  https://git.heroku.com/neurorehabilitation.git (fetch)
heroku  https://git.heroku.com/neurorehabilitation.git (push)
origin  https://bgarcial@bitbucket.org/bgarcial/neurorehabilitation_projects.git (fetch)
origin  https://bgarcial@bitbucket.org/bgarcial/neurorehabilitation_projects.git (push)
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗

部署到 Heroku 代码仓库

(nrb_dev)   neurorehabilitation_projects git:(master)  git push heroku master 
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 1.17 KiB | 0 bytes/s, done.
Total 13 (delta 11), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Using set buildpack heroku/python
remote: -----> Python app detected
remote:      $ pip install -r requirements.txt
remote: 
remote:      $ python manage.py collectstatic --noinput
remote:        Found another file with the destination path 'admin/css/forms.css'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
remote:        147 static files copied.
remote: 
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 46.1M
remote: -----> Launching...
remote:        Released v15
remote:        https://test-nrb.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy.... done.
To https://git.heroku.com/test-nrb.git
   9f0d32f..fcc62c9  master -> master
(nrb_dev)   neurorehabilitation_projects git:(master)  

7. 执行迁移过程

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python manage.py migrate
Running python manage.py migrate on test-nrb.... up, run.1678
System check identified some issues:

WARNINGS:
medical_encounter_information.RehabilitationSession.date_session_begin: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
medical_encounter_information.RehabilitationSession.date_session_end: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
userprofiles.User.birth_date: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Operations to perform:
  Apply all migrations: medical_encounter_information, userprofiles, contenttypes, auth, admin, sessions
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) ✗ 

8. 我在平台上部署的应用程序中创建了我的超级用户

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python manage.py createsuperuser
Running python manage.py createsuperuser on test-nrb.... up, run.7499
Username: bgarcial
Email address: bgarcial@eafit.edu.co
Password: 
Password (again): 
Superuser created successfully.
(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 

就是这样了。通过之前的步骤,我们可以在 Heroku 上部署 Django 应用程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接