Comma seperated Environment Varilables

evrenbingolHOBBY

4 months ago

I am creating commo seperated environment variables in Django for ALLOWED_HOSTS or CORS_ALLOWED_ORIGINS or CSRF_TRUSTED_ORIGINS.
I create them like so ALLOWED_HOSTS="site.com,mysite.com"
then I splite them on "," to get an array of ALLOWED_HOSTS
I always get a strange samicolon in the array

I have printed out values.

CSRF_TRUSTED_ORIGINS BEFORE SPLIT

https://staging.marvels.club,https://www.staging.marvels.club

CSRF_TRUSTED_ORIGINS AFTER SPLIT

['https://staging.marvels.club';, 'https://www.staging.marvels.club';]

I split like so

CSRF_TRUSTED_ORIGINS= getenv(
    'CSRF_TRUSTED_ORIGINS',''
).split(',')

this works woth .env files localy.

3 Replies

4 months ago

This isn't an issue with the Railway platform, instead an issue with your code.. how are you printing the variable, and what is that empty single quoted string for?


evrenbingolHOBBY

4 months ago

empty string is the default value to read if CSRF_TRUSTED_ORIGINS not found in environment .
I literally just do print(CSRF_TRUSTED_ORIGINS) and this suppose to print variables.
I do not add or append ";" anywhere.

['https://staging.marvels.club';, 'https://www.staging.marvels.club';]
this is the result of the split operation. For some reason reading the data from environment variable attaches ';'

If I do not split
Like so :

CSRF_TRUSTED_ORIGINS= getenv( 'CSRF_TRUSTED_ORIGINS','' )

And print this. I get the right value but as soon as
I do something like this

CSRF_TRUSTED_ORIGINS= [getenv( 'CSRF_TRUSTED_ORIGINS','' )]

wrap it in an array

there is an appended ";" after the value

So something like this

['https://staging.marvels.club';]

but to be honest when I print
all environvariables

like so

env_vars = os.environ

# Print each key-value pair
for key, value in env_vars.items():
    print(f"{key}={value}")

It prints out without the semicolon. So I think I do not have to worry about this.

but it is a strange case, It does not happen locally when I load .env or on an EC2 instance with .env files


4 months ago

If you notice the semicolon is not within the single quoted string.

Either way, this isn't something we could help with as it is not related to our platform.


Comma seperated Environment Varilables - Railway Help Station