Django Website Deploy 403 error

Hi, So basically I deployed my django project onto railway. The website loads fine and everything. However, when I try to login (I have a login feature on my website), it shows
Forbidden (403)
CSRF verification failed. Request aborted.
Origin checking failed - https://web-production-c9a.up.railway.app does not match any trusted origins.
Forbidden (403)
CSRF verification failed. Request aborted.
Origin checking failed - https://web-production-c9a.up.railway.app does not match any trusted origins.
Obviously, everything works fine locally as I have tested so not sure why is showing this when I deployed it. If anyone could help that'll be great!
51 Replies
Percy
Percy2y ago
Project ID: N/A
xRapids
xRapids2y ago
N/A
xRapids
xRapids2y ago
Yeah nevermind addiing CSRF_TRUSTED_ORIGINS = ['https://web-production-c9a.up.railway.app'] in the settings.py made it work. ty However, I got another question for the starter plan free. I see the default django sqlite works, reading and writing. Would that database save and not get deleted as in when leaving the website how it is, without redeploying from github etc.
Brody
Brody2y ago
if nothing ever triggers a redeploy, then yes your sqlite db will stay untouched, but frankly that is a horrible way of going about it when django makes it very easy to work with external databases like mysql or postgre
xRapids
xRapids2y ago
I see, thanks for the response. Yeah I was going to do postgre but then having to transistion the database with the data + all the media images files, seemed that it would take time so didnt get around to do it. And also what does the 500 execution hours mean. Like once I deployed or like 500 hours in total when someone is on the website itself?
Brody
Brody2y ago
500 hours depletes as long as you have at least 1 deployment active
xRapids
xRapids2y ago
Oh so does it basically mean that I have 21 days where the website would be live?
Brody
Brody2y ago
exactly
xRapids
xRapids2y ago
and in relation to the developer plan. How does it work. Lets say I am just deploying it, its not used much at all, only a few times what would be the pricing or
Brody
Brody2y ago
deploy it on the free plan, after a few days of looking at the metrics you will get an idea of how much resources your app uses, then plug them into the pricing estimator https://railway.app/pricing
xRapids
xRapids2y ago
Thanks. Another question, I added a custom domain. However, it shows as not secure without https How could I make it so it is secure bcz otherwise it shows as site unsafe, do you want to continue in order to access the website
Brody
Brody2y ago
just wait a few hours, sometime it just takes a bit
xRapids
xRapids2y ago
Yeah :). So I created a postgres as database. Added the needs in the settings.py installed psycopg2 etc. The postgres seemed all okay. However when I try deploy my django app from github, its just stuck on constant deployment in progress
Brody
Brody2y ago
Delete the deployment and try again
xRapids
xRapids2y ago
yeah I tried that two times now but still issue
Brody
Brody2y ago
#🚨|incidents
xRapids
xRapids2y ago
Does the estimated part mean for the month estimate?
xRapids
xRapids2y ago
How can I use the variables in the railway project. And how would it work with django project? I have a credentials.json which i dont want to put on the public repo as sensitive info, how can I still be able to use it as railway uses github files
Brody
Brody2y ago
put the secrets in the variables tab in the service then reference them in code as an environment variable
xRapids
xRapids2y ago
thanks for the reply. So this is where it references the credentials.json
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
which is a file that has all the info "private_key_id": "8030484a7f6f61db2c805" etc theres more of them. If I set each variable on that in the json file on the variables tab. What would I need to change this to?
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
jackson
jackson2y ago
erm i hope that private key isn't sensitive
xRapids
xRapids2y ago
i changed it dw, just to show as an example
Brody
Brody2y ago
os.environ['private_key_id'] that's how you would access it
xRapids
xRapids2y ago
yeah i understand how i would access individually But bcz this code, references a whole json file, which has many key variables and values in that json file, not sure what I would change this to os.path.join(BASE_DIR, 'credential.json')
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
Brody
Brody2y ago
too complicated, just wherever you need to access that private_key_id use os.environ['private_key_id'] and get rid of this JSON loader thingy
xRapids
xRapids2y ago
that was just one key within the credentials json file. But i believe i need everything in the json file for the code to work
from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
DEFAULT_FILE_STORAGE='EnergyProject.gcloud.GoogleCloudMediaFileStorage'
GS_PROJECT_ID = 're-energise-380012'
GS_BUCKET_NAME = 're-energise'
MEDIA_ROOT = "media/"
UPLOAD_ROOT = 'media/uploads/'
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_BUCKET_NAME)
from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'credential.json')
)
DEFAULT_FILE_STORAGE='EnergyProject.gcloud.GoogleCloudMediaFileStorage'
GS_PROJECT_ID = 're-energise-380012'
GS_BUCKET_NAME = 're-energise'
MEDIA_ROOT = "media/"
UPLOAD_ROOT = 'media/uploads/'
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_BUCKET_NAME)
Credentials.json file inside looks liek this: (edited the sensitive values)
{
"type": "service_account",
"project_id": "re-energi",
"private_key_id": "8030484a7f6f61db25c7",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB\n-----END PRIVATE KEY-----\n",
"client_email": "re-energise@re-e",
"client_id": "1119152ljh",
"auth_uri": "h",
"token_uri": "https://oauth",
"auth_provider_x509_cert_url": "https://www.",
"client_x509_cert_url": "https://www.googleap"
}
{
"type": "service_account",
"project_id": "re-energi",
"private_key_id": "8030484a7f6f61db25c7",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB\n-----END PRIVATE KEY-----\n",
"client_email": "re-energise@re-e",
"client_id": "1119152ljh",
"auth_uri": "h",
"token_uri": "https://oauth",
"auth_provider_x509_cert_url": "https://www.",
"client_x509_cert_url": "https://www.googleap"
}
Brody
Brody2y ago
oh it's this crap
jackson
jackson2y ago
ah yes google apis
Brody
Brody2y ago
pain
jackson
jackson2y ago
google changed up their forms API literally 4 days ago bricking a feature we were almost ready to ship and now im having to write some crazy fetch commands with sus headers to make it possible to do something they're not supporting in anything but their proprietary google app script
xRapids
xRapids2y ago
rip
Brody
Brody2y ago
I only use the Google APIs that requires 1 API key as a header / query param
jackson
jackson2y ago
regarding this, every time i've dealt with this i just plopped the credentials.json file in the ~/.config/gcloud directory and then the python application just picked it up and ran with it but this was assuming that gcloud cli was installed
Brody
Brody2y ago
could they store it in a secure bucket and download the file at run time?
xRapids
xRapids2y ago
hm
jackson
jackson2y ago
yeah that is a workaround, assuming you can't just extract the fields in the json file and use them as env vars -> somehow set them in that library's config
Brody
Brody2y ago
yeah but there's a big private key
jackson
jackson2y ago
shouldn't be an issue right? just a long string
Brody
Brody2y ago
I'm sure railway has a limit of the amount of characters in a key or value, right?
jackson
jackson2y ago
ah, not sure, never ran up against that
Brody
Brody2y ago
trys to update a value with 300 chars and takes down the backboard
xRapids
xRapids2y ago
Could I do this?
from google.oauth2 import service_account
import os

# Get the environment variables
GS_PROJECT_ID = os.environ.get('GS_PROJECT_ID')
GS_PRIVATE_KEY_ID = os.environ.get('GS_PRIVATE_KEY_ID')
GS_PRIVATE_KEY = os.environ.get('GS_PRIVATE_KEY')
GS_CLIENT_EMAIL = os.environ.get('GS_CLIENT_EMAIL')
GS_CLIENT_ID = os.environ.get('GS_CLIENT_ID')
GS_AUTH_URI = os.environ.get('GS_AUTH_URI')
GS_TOKEN_URI = os.environ.get('GS_TOKEN_URI')
GS_AUTH_PROVIDER_X509_CERT_URL = os.environ.get('GS_AUTH_PROVIDER_X509_CERT_URL')
GS_CLIENT_X509_CERT_URL = os.environ.get('GS_CLIENT_X509_CERT_URL')

# Create the credentials object using the environment variables
GS_CREDENTIALS = service_account.Credentials.from_service_account_info({
"type": "service_account",
"project_id": GS_PROJECT_ID,
"private_key_id": GS_PRIVATE_KEY_ID,
"private_key": GS_PRIVATE_KEY.replace('\\n', '\n'),
"client_email": GS_CLIENT_EMAIL,
"client_id": GS_CLIENT_ID,
"auth_uri": GS_AUTH_URI,
"token_uri": GS_TOKEN_URI,
"auth_provider_x509_cert_url": GS_AUTH_PROVIDER_X509_CERT_URL,
"client_x509_cert_url": GS_CLIENT_X509_CERT_URL
})
from google.oauth2 import service_account
import os

# Get the environment variables
GS_PROJECT_ID = os.environ.get('GS_PROJECT_ID')
GS_PRIVATE_KEY_ID = os.environ.get('GS_PRIVATE_KEY_ID')
GS_PRIVATE_KEY = os.environ.get('GS_PRIVATE_KEY')
GS_CLIENT_EMAIL = os.environ.get('GS_CLIENT_EMAIL')
GS_CLIENT_ID = os.environ.get('GS_CLIENT_ID')
GS_AUTH_URI = os.environ.get('GS_AUTH_URI')
GS_TOKEN_URI = os.environ.get('GS_TOKEN_URI')
GS_AUTH_PROVIDER_X509_CERT_URL = os.environ.get('GS_AUTH_PROVIDER_X509_CERT_URL')
GS_CLIENT_X509_CERT_URL = os.environ.get('GS_CLIENT_X509_CERT_URL')

# Create the credentials object using the environment variables
GS_CREDENTIALS = service_account.Credentials.from_service_account_info({
"type": "service_account",
"project_id": GS_PROJECT_ID,
"private_key_id": GS_PRIVATE_KEY_ID,
"private_key": GS_PRIVATE_KEY.replace('\\n', '\n'),
"client_email": GS_CLIENT_EMAIL,
"client_id": GS_CLIENT_ID,
"auth_uri": GS_AUTH_URI,
"token_uri": GS_TOKEN_URI,
"auth_provider_x509_cert_url": GS_AUTH_PROVIDER_X509_CERT_URL,
"client_x509_cert_url": GS_CLIENT_X509_CERT_URL
})
jackson
jackson2y ago
yeah that's what i was thinking
xRapids
xRapids2y ago
Yeah the private key is mad longg very
Brody
Brody2y ago
yeah don't break the environment variable display again please that's what happened last time it broke, someone must have tried to store a private key (joke) chuck it in a secure cloudflare r2 bucket and download the file in code before referencing the file then you'd only need the secrets for the cloudflare r2 api access
xRapids
xRapids2y ago
alright thanks, ill try this out first and see if works
Brody
Brody2y ago
please dont even if you can store the private key in a environment variable, it's a half assed hack
xRapids
xRapids2y ago
not fully sure how i would do this
chuck it in a secure cloudflare r2 bucket and download the file in code before referencing the file then you'd only need the secrets for the cloudflare r2 api acces
Brody
Brody2y ago
store JSON file in bucket, use some library to connect to bucket, download file, then use file
Want results from more Discord servers?
Add your server