'why is dj-database-url throwing me an error of : a bytes-like object is required, not 'str' and how do i get around this?

so i am a student attempting to create a django based app...my app is finished and im wanting to launch it on heroku. The error message comes when i run: python3 manage.py makemigrations

I am using dj-database-url for the db url and have the env stored as :

DATABASES = {'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))}

The versions of everything I am using are:

chardet==3.0.4
dj-database-url==0.5.0
Django==2.2.7
heroku==0.1.4
idna==2.8
psycopg2==2.8.4
python-dateutil==1.5
pytz==2019.3
requests==2.22.0
sqlparse==0.3.0
stripe==2.40.0
urllib3==1.25.7

and the traceback error message I am receiving is :

  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Jakey Poo\PycharmProjects\artwork\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Jakey Poo\PycharmProjects\artwork\venv\lib\site-packages\django\core\management\__init__.py", line 325, in execute
    settings.INSTALLED_APPS
  File "C:\Users\Jakey Poo\PycharmProjects\artwork\venv\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
    self._setup(name)
  File "C:\Users\Jakey Poo\PycharmProjects\artwork\venv\lib\site-packages\django\conf\__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\Jakey Poo\PycharmProjects\artwork\venv\lib\site-packages\django\conf\__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\Jakey Poo\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Jakey Poo\PycharmProjects\artwork\artwork\settings.py", line 89, in <module>
    DATABASES = {'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))}
  File "C:\Users\Jakey Poo\PycharmProjects\artwork\venv\lib\site-packages\dj_database_url.py", line 80, in parse
    if '?' in path and not url.query:
TypeError: a bytes-like object is required, not 'str'

The code my app is getting stuck on in dj-database-url is :

    path = url.path[1:]
    if '?' in path and not url.query:
        path, query = path.split('?', 2)
    else:
        path, query = path, url.query
    query = urlparse.parse_qs(query)

I have never used dj-database-url before and im still relatively new to django and python.

What I have tried:

  1. I have checked that all my software versions are up to date and compatable
  2. I have tried messing with my db url, giving it quotes, removing quotes etc.
  3. I have tried changing the syntax in my settings.py from { to [ as suggested by another post.
  4. I have tried printing path to see what it returns, but it returns nothing ( I could be putting the print statement in the wrong place, though)

If anyone is needing anymore information please just ask, I think i have covered everything i can already, though.



Solution 1:[1]

I'm not sure exactly where the problem lies in dj_database_url but reading through their github issues I think it's a problem with some characters contained in the DATABASE_URL environment variable.

In your settings.py try using this instead of dj_database_url.parse():

... other config settings ...
DATABASE_URL = os.getenv('DATABASE_URL')
DATABASES = {
    'default': dj_database_url.config(),
}

config() should automatically use the DATABASE_URL setting by default, so simply setting it from your environment variable first and then calling config() after should bypass whatever problem the parse() method in handling the url.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pspahn