In settings.py, I get var from environment like this:
ROBOTS_STR = os.environ.get('DJANGO_ROBOTS_STR')
My env var is set in a file and looks like:
DJANGO_ROBOTS_STR=User-agent: *\nDisallow: /admin\nDisallow: /api
The problem is that in the view, when I get settings.ROBOTS_STR, the value of the string has been auto escaped. It is: User-agent: *\\nDisallow: /admin\\nDisallow: /api
How can I change this behaviour? Note that I'm using Python 3.3
Decode it with string-escape:
>>> os.environ.get('DJANGO_ROBOTS_STR')
'User-agent: *\\nDisallow: /admin\\nDisallow: /api'
>>> os.environ.get('DJANGO_ROBOTS_STR').decode('string-escape')
'User-agent: *\nDisallow: /admin\nDisallow: /api'
>>> print(os.environ.get('DJANGO_ROBOTS_STR'))
User-agent: *\nDisallow: /admin\nDisallow: /api
>>> print(os.environ.get('DJANGO_ROBOTS_STR').decode('string-escape'))
User-agent: *
Disallow: /admin
Disallow: /api
For Python 3, encode it first, then decode it:
>>> os.environ.get('DJANGO_ROBOTS_STR').encode('latin1').decode('unicode_escape')
'User-agent: *\nDisallow: /admin\nDisallow: /api'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With