Deploy static assets to heroku from local machine

2019-02-18 17:16发布

I am trying deploy my assets files to heroku and I get this output in my command line interface:

(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) ✗ 

It's strange for me, due to I am currently in the directory/folder in which the manage.py file is located

With the order to get an application early deployment, I've choose disable the environment variable DISABLE_COLLECT_STATIC of this way:

(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)

According to the above, when I perform git push heroku master I can deploy mi application:

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) ✗

According to the previous actions, in my main link https://neurorehabilitation.herokuapp.com/ there is an error, I guess that it's because the static files des not were sent and were disabled.

Is this right? I don't know ...

But, something that seem strange is that in my Django Admin https://neurorehabilitation.herokuapp.com/admin/ I can detail the template and the css styles and js in the admin, then, I unknown if heroku is working with static files or not.

What happened here int this situation with my static files?

However, I follow my process, and in this opportunity I can deploy remotely to heroku actions related with the manage.py commands.

I've checked this because I can execute my migrations of my application:

(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)

Also, I ccan create inside my deployed application a superuser from my local toolbet ...

(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) ✗ 

After, I've disabled the environment variable setup previously DISABLE_COLLECTSTATIC=1 (I've delete the environment variable from my settings dashboard heroku application)

But, wen I execute again the heroku run python manage.py collectstatic command, I get this notification:

(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) ✗

Checking this inconvenient, I guess that my error is here at the end:

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'

And, this tell me that in my heroku application deployed does not exist the static directory

I've executed heroku run bash for check the structure directory inside my heroku application and I can detail the root project named /app in heroku

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run bash    
    Running bash on neurorehabilitation.... up, run.5840
    ~ $ pwd 
    /app
    ~ $ 

I listed the /app content and I see all my directories and files of a similar way like I have in my development local environment django project

~ $ 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

With my error is related with this subject:

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

Then I enter to these directory /app/neurorehabilitation/settings/ but I can see that does not exist a directory named static inside, that is the way as heroku try search:

~ $ cd /app/neurorehabilitation/settings/
~/neurorehabilitation/settings $ ls
base.py  development.py  __init__.py  production.py  staging.py  testing.py
~/neurorehabilitation/settings $

And this is because according to the structure directory that I setup when I create my django project in my local development environment. I define the following:

enter image description here

Then, according to the previous situation ...

Why git when I perform deployment to heroku, the process is searching a path to neurorehabilitation/settings/static when this is not setup from the originar sproject source, this mean, my project in my machine before to deploy to heroku?

What action could I take about it? Heroku platform, when I execute the heroku run python manage.py collectstatic command this try search a directory taht does not exist or not is setup in the structure directory of my project?

Even I try create the /app/neurorehabilitation/settings/staticfolder via heroku run bash, but this not works

(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)

I unknown how to address this problem, Why in my heroku deployment does not create the structures directory similar to my local project?

Somebody what is the reason about it?

Is possible that in the moment I still not understad well how to works heroku at deployment instant in relation to structure directory and files when these are upload to the platform via git push heroku master?

Any support will be highly appreciated.

Thanks for the orientation :)

2条回答
劫难
2楼-- · 2019-02-18 17:33

I will show in this answer step by step what I made for deploying my Django application in Heroku. I hope this question be useful for the others also would like to say thanks to the people that spend time helping me with this

1. Setup my settings for my static files in heroku

  • settings configurations to Heroku

The key of that is to be able to deploy my static files to Heroku was in my settings/base.py, acccord also to the said by ahmed and Oz123 in their responses.

Particulary, I had an issue was in the value of PROJECT_ROOT directive. Heroku suggest that PROJECT_ROOT directive of this way:

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

I did the setup PROJECT_ROOT (I denomined BASE_DIR in my settings/base.py) as says Heroku suggested, but an error always appeared, then I took the option of leaving BASE_DIR directive as Django has by default like my PROJECT_ROOT directive in relation to the heroku configuration referenced above.

Error I was having:

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

when I had the BASE_DIR as:

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

Finally, my settings/base.py file, the content was:

# 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/'

As my BASE_DIR (PROJECT_ROOT in heroku ) I defined the file system path, finally my STATICFILES_DIRS directive also was compromised like Oz123 did reference me in their response. :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"),
)

Then according to the deployment settings in my project (pink square in this image) my settings/production.py contains:

# ------ *** -------------
# 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 = ['*']

Is necessary also to install the dj-database-url package in my virtual environment:

(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) ✗

Add the dj-database-url package/dependence in the requirements/production.txt file of this way:

dj-database-url==0.4.0

This was my inconvenient for which the collectstatic command did not work when heroku deployed the application and trying to copy and processing my assets to amazon S3 where I have my static files according with my settings configuration


In addition, I wanted to share my heroku deployment process I think this will be useful or even anecdotic

For deploy a Django application to heroku, is necessary have in the project root the following files:

  • requirements.txt file

I had the myproject/requirements/ folder in the root of my Django project, but the heroku deployment process does not works of this way

Then I create in the same level or hyerarchy of requirements/ folder a requirements.txt file with the following content:

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

And my requirements/production.txt file has been stayed so:

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

The requirements/base.txt file have all dependences or packages necessary in my project.

I needed Heroku install the 3.4 python version in the deployment process, then was necessary to indicate it in the runtime.txt file (created in my project root) of this way

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

A Procfile is a text file in the root directory of your application that defines process types and explicitly declares what command should be executed to start your app.

My Procfile has been stayed so:

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

2. Testing the access from my machine through heroku toolbet

Is necessary install the Heroku toolbet

which provide access to the Heroku command Line Interface which is used for managing and scaling the applications and addons, etc.

I test the access to heroku from my machine of this way:

(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. Creating my heroku application

Accord to this response, as the application to deploy is python/Django the heroku application should be create of this way:

(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) ✗ 

Then, of this way my heroku application is created in my web dashboard

enter image description here

4. Setup the environment variables in my project for Heroku

I have some environment variables in my local project used for hidden my secret keys, database user, name and password, some access to my amazon web services and s3 account and the setings used for production server (DJANGO_SETTINGS_MODULE)

Is necessary indicate to Heroku the way in that he will know these environments variables.

Accord to this link we will configure the vars for a deployed application. Then I've setup in heroku my environment variables of this way:

  • DJANGO_SETTINGS_MODULE

My deployment will be guided for the settings/production.py file which inherit from settings/base.py which have all the application domain packages/dependences. Then my DJANGO_SETTINGS_MODULE variable in heroku stay so:

(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

I execute:

(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

I execute:

(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

I execute:

(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 : Value in single quotes

I execute:

(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 Access Key ID : Value in single quotes

I execute:

(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 : Value in single quotes

I execute:

(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 Storage Bucket name : Value in single quotes

I execute:

(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) ✗ 

Then according to above options in my web dashboard the environment variables conatins:

enter image description here

5. Execute commit operation to the heroku repository

First we check the files to be committed

(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

Make the commit operation

(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. Deploy our code application to heroku repository

Before, I check my remotes repos. I have reference the heroku alias

(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) ✗

Deploy to heroku repository

(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. Execute the migrations process

(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. I create my super user in my application deployed in the platform

(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) ✗ 

And That's all. With the previous process we can deploy on heroky a Django application

查看更多
Viruses.
3楼-- · 2019-02-18 17:38

Have you properly defined:

STATICFILES_DIRS

Inside your settings.py?

Have you created it locally and added it to git (as empty directory...). The trace you have indicates that this folder is missing. I have created a simple project with the following settings:

~/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'),)

As you can see the STATICFILES_DIRS contains only one directory, which does not exist:

~/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

The command collectstatic fails:

~/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'

I hope this will lead you to solving your problem.

查看更多
登录 后发表回答