DEV Community

Cover image for USING RAILWAY APP TO DEPLOY YOUR DJANGO PROJECT

USING RAILWAY APP TO DEPLOY YOUR DJANGO PROJECT

Osahenru on July 03, 2022

Alright guys, at the end of this article I hope you should be able to deploy your first Django project on Railway app. Railway app is a cloud plat...
Collapse
 
andemusgbj profile image
Albert Gubanja

Hi, How can I solve the CSRF on Railway.
Everything is doing well in local. I can even access to my online Database and push or pull data in local. But it is not possible in production. The mater is that I cannot login to my admin page. I can see the login page, but I cannot login. When I try to login, here is the error that is occuring.

Forbidden (403)
CSRF verification failed. Request aborted.

Help
Reason given for failure:

Origin checking failed - https://andemus.up.railway.app does not match any trusted origins.

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django’s CSRF mechanism has not been used correctly. For POST forms, you need to ensure:

* Your browser is accepting cookies.
* The view function passes a request to the template’s render method.
* In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
* If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
* The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login.
Enter fullscreen mode Exit fullscreen mode

What can I do to fix this problem on railway?

Collapse
 
magicrodri profile image
Rodrigue Roland CODO

Need to set in settings:


CSRF_TRUSTED_ORIGINS = [
    'https://your-base-domain'
]

Enter fullscreen mode Exit fullscreen mode
Collapse
 
joseantoniomanjarres profile image
joseantoniomanjarres

Hi, How to know my "your-base-domain"

Thread Thread
 
yabesh12 profile image
Yabesh

"your-base-domain" is your project name

Thread Thread
 
magicrodri profile image
Rodrigue Roland CODO

You base domain is the root address of your app. In railway you find it in your domains setting for you django application.

Collapse
 
andreportol profile image
André L. Porto

I have the same problem! What did you do to solve it? thanks

Collapse
 
andemusgbj profile image
Albert Gubanja

I didn't be able to solve it.
Until now I am still looking for a solution

Thread Thread
 
andreportol profile image
André L. Porto

Thanks for the feedback. If I find the problem, I'll post the solution.

Thread Thread
 
andemusgbj profile image
Albert Gubanja

Maybe we should make a short call for verification. We need your help really

Thread Thread
 
andreportol profile image
André L. Porto

I was able to resolve the csrf_token issue just by replacing the Django version.
The version I was using was Django==4.1.7 and I switched to Django==3.2.17.

Collapse
 
catsarebaddogsaregood profile image
catsarebaddogsaregood

Actually i tried "CSRF_TRUSTED_ORIGINS = ["xxxxxxxxx.railway.app"]",i still can't create data in the Django admin,then i try to add "django-cors-headers" in the requirements.txt,then it's work for me :)

Collapse
 
guitaralgorithman profile image
Gustavo Henriquez

I have a error about utf-8

Collapse
 
guitaralgorithman profile image
Gustavo Henriquez

Error: stream did not contain valid UTF-8

Collapse
 
osahenru profile image
Osahenru

I guess it's an error from your HTML file, we'll do justice to properly debugging if I can see a snippet of your code

Thread Thread
 
ricardopasqualino profile image
_pazqua

How can I do that?

Collapse
 
iconnell profile image
McConnell

I have this error too. how did you solve it

Thread Thread
 
nigismo profile image
Nigismo

Use whitenoise

Collapse
 
iconnell profile image
McConnell

I had this error too, change the file encoding for requirements.txt to utf-8

Collapse
 
layou32 profile image
layou32

This work for me, thank you so much

Collapse
 
francisbilla profile image
Francis Billa

can you correct the typo: pip install gunicorn

Collapse
 
osahenru profile image
Osahenru

Fixed. Thank you for your review

Collapse
 
samydev profile image
Samilly da Silva Nunes

Thank you so much! Really good article

Collapse
 
fiorins profile image
Davide Fiorini • Edited

seems it works only if debug=1, with debug=0 it doesn't see static files

Collapse
 
ghostclonelol2000 profile image
<}:-{~ .A.K.a. DOOM

Aitec/aitx (gatx)

Collapse
 
andemusgbj profile image
Albert Gubanja

I need a help. I am meeting an issue with static files.
Here is my settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
Enter fullscreen mode Exit fullscreen mode

After collecting staticfiles and pushing the project to GitHub, Railway can't find the static files, even if they have been pushed on GitHub. Can anyone help me?

In local, everything is working ok.

Image description

Collapse
 
andemusgbj profile image
Albert Gubanja

Everything is ok now.
The whitenoise was missing. I added the whitenoise to the middleware and everything is ok now.
Thanks

Collapse
 
osahenru profile image
Osahenru

Thanks for your feedback

Collapse
 
rogendo profile image
Peter

I get this error on my deployed Django webapp. I used bootstrap and also customized my own CSS but the deployed webapp does not seem to find this style.css.

Not Found: /static/css/style.css

My static settings are:

STATIC_URL = 'static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static")]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Collapse
 
foxinabox profile image
Fox-In-A-Box

I keep getting this error and then it crashes. I have gunicorn in the requirements.txt file and have the Procfile setup as instructed. Not sure what I'm doing wrong
Image description

Collapse
 
sebherrerabe profile image
Sebastian Herrera

i had the same. In the step 2, the format of the requirements file should be ".txt" so the command should be pip freeze > requirements.txt

Collapse
 
foxinabox profile image
Fox-In-A-Box

Yeah, my requirements file is requirements.txt but I'm still getting the same error

Collapse
 
sagar248 profile image
Sagar Prateek

I faced a similar error. Use web: gunicorn 'name-of-application.wsgi' in Procfile, and deploy in a new service because the deploy configurations gets saved.

Collapse
 
engdanilo profile image
Danilo Santana • Edited

Hi, guys!
I have the same problem with my application. When I run local, it works perfectly. But after deploy on Railway, it does not find the static files.
I tried to use whitenoise in middleware but the problem continues.
I have other site runs good, but my second one has this problem and I don’t know how solve.
My Code can be seen on my github
engdanilo/estetica

Collapse
 
md_shantoujar_493b72da0 profile image
Md. Shanto Ujar

It says no deploys for this service.
Image description

Image description

Collapse
 
viestar profile image
Sylivester Mberenge

got this error

11 ERROR: executor failed running [/bin/bash -ol pipefail -c python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt]: exit code: 1


Do i have to upload my Venv folder?

Collapse
 
viestar profile image
Sylivester Mberenge

had it resolved, had included a requirement "pywin" which couldn't be installed

Collapse
 
nguyenbathanh profile image
Thanh Nguyen

How do you install GDAL on Railway?

Collapse
 
joshthecodingaddict profile image
Joshua Hassan

my media files dont show when i upload an image on the admin

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Enter fullscreen mode Exit fullscreen mode

my settings

Collapse
 
arslanamin404 profile image
Arslanamin404

Video tutorials

Collapse
 
guidias2002 profile image
guidias2002

Image description

I have this error, does anyone know what to do?

Collapse
 
fallou55 profile image
fallou

i have the same problem can you help me please???

Collapse
 
hlruffo profile image
Hugo L. Ruffo

Did anyone else had problems with the admin ?
After the deploy everything works fine but the admin template is absolutely broken

Collapse
 
shaysframe profile image
Shay

I'm getting a server error status on my project. I wonder why... sigh

Collapse
 
lukadsant profile image
Lucas

thanks, this post was a great help ^^

Collapse
 
mungaben profile image
mungaben

django DRF CSRF ERROR .How do you fix it guys?

Collapse
 
sgafgfjsa profile image
uwuwuw

May I ask? How did you solve your $PORT issue in Railway? I used sqlite also.

Collapse
 
torjo profile image
Faysal Hassan Torjo

I am facing this Problem while deploying

Image description

Collapse
 
tejasnirmal123 profile image
Tejas Nirmal

I have an error of ..... Template does not exist
Code is running perfect on local compute but giving error while deploying. Any solution ?

Collapse
 
ribeirov1 profile image
Vitor Ribeiro

Image description
what this mean?

Collapse
 
tanatrox profile image
Daniel Galindo

someone has the same issue and how could fix this error.

Image description

Collapse
 
ayorex11 profile image
Ayomide Adewale

Hi, so I deployed my Django rest framework API project to railway and I've been getting a cors header error

Collapse
 
ayorex11 profile image
Ayomide Adewale

Any idea of how I can fix it?