Heroku Database Settings Injection - How do I setup my dev django database?

前端 未结 5 1790
孤城傲影
孤城傲影 2021-01-30 04:32

I\'m trying to get my local dev django app to work after following these instructions on adding env database settings.

https://devcenter.heroku.com/articles/django-injec

相关标签:
5条回答
  • 2021-01-30 04:53

    import dj_database_url

    DATABASES = {'default': dj_database_url.config(default='postgres://yourusername:yourpassword@yourhosturl:5432/yourdbname')}

    ** Replace bold string with your database settings if you are using local database then replace yourhosturl by localhost

    0 讨论(0)
  • 2021-01-30 04:55

    Just set an environment variable on your operating system and check weither or not it's set. For instance, with a UNIX system:

    # In ~/.bash_profile
    export LOCAL_DEV=true
    
    # In settings.py
    import dj_database_url
    DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
    
    if bool(os.environ.get('LOCAL_DEV', False)):
        # Override DATABASES['default'] with your local database configuration
    

    Also, if you need to set an environment variable on your heroku space:

    heroku config:add MY_VAR='my_value'
    
    0 讨论(0)
  • 2021-01-30 04:58

    You can just add your dev settings to the default values like this...

    import dj_database_url
    DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar@localhost:5432/db')}
    
    0 讨论(0)
  • 2021-01-30 05:02

    I just tried this and here is my code:

    import dj_database_url
    
    local_db = 'postgres://django_login:123456@localhost/django_db'
    DATABASES = {'default': dj_database_url.config(default=local_db)}
    

    My database name is "django_db", user name is "django_login", password is "123456".

    My code can run both in local machine and heroku.

    0 讨论(0)
  • 2021-01-30 05:14

    Use this in your settings.py:

    DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])}
    

    and in your .env file have this:

    DATABASE_URL=postgres://localhost/yourdbname
    

    when you launch with "foreman start" it will look at the .env file and create all those environment variables, just like running on Heroku itself. Type "heroku config" to confirm that you have a DATABASE_URL set, which you should if you added the postgres database addon.

    0 讨论(0)
提交回复
热议问题