DEV Community

Cover image for USING RAILWAY APP TO DEPLOY YOUR DJANGO PROJECT
Osahenru
Osahenru

Posted on • Edited on

USING RAILWAY APP TO DEPLOY YOUR DJANGO PROJECT

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 platform that gives you simple configured free deploys, removing common developer hurdles with zero CLI, just to add as a bragging rights it has a faster build time than Heroku, interestingly I discovered it while I was having issues deploying my project to Heroku check here to see how I did that.

All you need to get started is your GitHub repo. Railway has some starter applications which can be deployed on the go, and it also comes with a PostgreSQL as the default database manager.

In this article an explanation to some of the terminologies used here are not given, it is assumed that the reader must be familiar with them if not check out this article where I explain these terminologies in details.

With that much said let us dive in, using these steps i'll show you how to deploy your Django projects on railway.

Ensure you already have a Railway account if don't you can sign-up here, A GitHub account and the project you want to deploy is been cloned once you’re done these 3 basic things follow these steps.

Step 1.
pip install gunicorn

Step 2.
pip freeze > requirements

Step 3.
Create a Procfile in your root folder and save the following lines of codes in it
web: gunicorn 'name-of-application.wsgi'
Note: A Procfile has no file extension

Step 4.
Create runtime.txt
Railway needs to know the version of python you used for your project, to know the version used type python --version in your terminal copy, paste and save the version inside your runtime.txt python -3.10.2

Step 5.
Next, we need to make some adjustments to our settings.py file
Look for the line that has

ALLOWED_HOST = [ ]

and change it to

ALLOWED_HOST = ['*']

Step 6.
Still on your settings.py file add the following lines of codes in your static section, so your static files can be properly rendered

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT =os.path.join(BASE_DIR, 'staticfiles')

Step 10.
Lastly, we collect our static files into one folder using the following command
python manage.py collectstatic

Next, we push our project to our Github account where we’ll be deploying our project from. Use the following git syntax to add, commit and push your code.
git status
git add .
git commit -m ‘customized commit message’
git push

Now, we move to our railway app account that was created

Click + New

How to add new project

GitHub Repo

Select your GitHub repo

select a repo and Railway pack helps you build and automatically deploy your project using nixpacks you can see the build status as it is been built, once that has been done,

Build status

click on your project go to the settings tab

Settings tab

under domain click generate domain

Generate domain

a customized link to your deployed app is been created you can visit it by clicking on it.

View generated domain

VOILA!!!
Your project has been deployed.
CONGRATULATIONS!!!

Top comments (52)

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