HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.83k stars 196 forks source link

Cannot set default value for SECURE_PROXY_SSL_HEADER #68

Closed ovalseven8 closed 4 years ago

ovalseven8 commented 6 years ago

In my settings file I have:

SECURE_PROXY_SSL_HEADER = config('SECURE_PROXY_SSL_HEADER', default=None, cast=Csv(post_process=tuple))

Now, when I want to set the header on one of my projects, everything is fine. I just put the following in my .env file:

SECURE_PROXY_SSL_HEADER=HTTP_X_FORWARDED_PROTO, https

However, when I do not need this header in another project, it does not work. Because if I leave empty, the default value is None and that conflicts with cast=Csv(post_process=tuple).

ovalseven8 commented 6 years ago

One solution is

config('SECURE_PROXY_SSL_HEADER', default=None, cast=lambda s: Csv(post_process=tuple).__call__ if s else None)

However, that looks hacky. Is not there a better way?

henriquebastos commented 4 years ago

So this is clearly a design mistake in Django. Instead of None it should use an empty tuple.

So to work around in a (maybe) less hacky form trye:

SECURE_PROXY_SSL_HEADER = config('SECURE_PROXY_SSL_HEADER', default='', cast=Csv(post_process=tuple)) or None

Also, decouple simplicity encourages you to express what make sense for your domain and you could also do:

def header(s, cast=Csv(post_process=tuple)):
    return cast(s) or None

config('SECURE_PROXY_SSL_HEADER', default='', cast=header)

Then you could have a collection of cast helpers for your domain.