How to use dj-database-url while connecting with postgresql in heroku using python

前端 未结 2 1415
不思量自难忘°
不思量自难忘° 2021-02-04 21:23

I\'m here because I\'m really really new with heroku-python-django-postgresql group. I have googled for a usage for dj-database-url and I don\'t understand why i have to use it

相关标签:
2条回答
  • 2021-02-04 22:07

    Use pip module dj-dtabase-url. Add an environmental var with the name 'DATABASE_URL' and the value from the heroku db settings.

    Important, to avoid the error 'NameError: name 'DATABASES' is not defined.' You still have to leave for example the default DATABSE settings.

    The complete code:

    import dj_database_url
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'mydatabase',
        }
    }
    DATABASES['default'] = dj_database_url.config()
    
    0 讨论(0)
  • 2021-02-04 22:17

    dj-database-url is a utility to help you load your database into your dictionary from the DATABASE_URL environment variable. Heroku uses environment variables for your database and other addons. To begin using your database you'd simply use the below command to setup your DATABASES dictionary:

    import dj_database_url
    DATABASES['default'] = dj_database_url.config()
    

    And maybe stash DATABASE_URL in your virtualenv activate script.

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