On 4 Nov 2020, at 03:45, Marc Johnson 
<marcjohnson...@gmail.com<mailto:marcjohnson...@gmail.com>> wrote:
...
1. Since this app is dockerized, I have my existing env vars in my 
docker-compose-prod.yml file. Is that where I should list DATABASE_URL? Like:

db:
    image: postgres:11
    volumes:
      - postgres_data:/var/lib/postgresql/data/
environment:
- DATABASE_URL=postgresql://<username>:<password>@<host>:<port>/<databasename>


Looks good, but to be certain jump into your container (exec or run) and verify 
that the DATABASE_URL is set correctly. It should be.


2. Does the below formatting look correct to you from my settings.py? I am 
running into issues with the env var DATABASE_URL and getting this error:  
Environment variable "{}" not set'

DATABASES = {
    'default': dj_database_url.config(env("DATABASE_URL"), 
default="postgres://postgres@db/postgres", conn_max_age=1800),
    'ENGINE': 'django.db.backends.postgresql',
}

There was a mistake in my previous response, so looks like that led you astray.

First, omit "ENGINE" here - unnecessary as postgres (or postgresql) scheme in 
the URL will drive that.

Second, you don't need to specify "DATABASE_URL" directly, that is the default 
variable used if you don't override it, or you can specify 'DATABASE_URL' 
there, or use env='DATABASE_URL'. Your choice.

If you do extract the url prior calling .config(), then use the  default= 
keyword, even though DATABASE_URL will override it. If you pass a positional 
argument to .config it will be used as the environment variable, not the value. 
If you have an already extracted value, then alternatively you can use .parse() 
instead of .config() which takes the url as the first positional arg.

dj_database_url is a pretty simple single module package. Check out the source 
code, it isn't too hard to comprehend.

DATABASES = {
    'default': dj_database_url.config(conn_max_age=1800)
}

should do the trick on heroku.

3. I am also getting this error when running locally. Do you have any advice 
for troubleshooting?

web_1  | [2020-11-03 16:35:59 +0000] [7] [ERROR] Error handling request /NDCs/
web_1  | Traceback (most recent call last):
web_1  |   File 
"/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in 
handle
web_1  |     self.handle_request(listener, req, client, addr)
web_1  |   File 
"/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 175, in 
handle_request
web_1  |     respiter = self.wsgi(environ, resp.start_response)
web_1  |   File 
"/usr/local/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 
131, in __call__
web_1  |     signals.request_started.send(sender=self.__class__, 
environ=environ)
web_1  |   File 
"/usr/local/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 
177, in send
web_1  |     return [
web_1  |   File 
"/usr/local/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 
178, in <listcomp>
web_1  |     (receiver, receiver(signal=self, sender=sender, **named))
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/db/__init__.py", 
line 46, in reset_queries
web_1  |     for conn in connections.all():
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", 
line 229, in all
web_1  |     return [self[alias] for alias in self]
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", 
line 229, in <listcomp>
web_1  |     return [self[alias] for alias in self]
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", 
line 211, in __getitem__
web_1  |     self.ensure_defaults(alias)
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", 
line 176, in ensure_defaults
web_1  |     conn.setdefault('ATOMIC_REQUESTS', False)
web_1  | AttributeError: 'str' object has no attribute 'setdefault'

I think this is the fallout from above, passing the url as the first positional 
instead of an environment variable name or just omitting it.




-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3BF1BFA9-00EB-40D2-9EED-5DF1B2313BD5%40uniquode.io.

Reply via email to