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!!!

Latest comments (53)

Collapse
 
ejike_orji_090570e6740269 profile image
EJIKE ORJI

INABILITY TO HAVE A RAILWAY APP

Nice guidelines. But i am having a problem with having a railway account which i need to deploy my django app. How can i overcome the challenge?

Collapse
 
md_shantoujar_493b72da0 profile image
Md. Shanto Ujar

It says no deploys for this service.
Image description

Image description

Collapse
 
samydev profile image
Samilly da Silva Nunes

Thank you so much! Really good article

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
 
nguyenbathanh profile image
Thanh Nguyen

How do you install GDAL on Railway?

Collapse
 
torjo profile image
Faysal Hassan Torjo

I am facing this Problem while deploying

Image description

Collapse
 
arslanamin404 profile image
Arslanamin404

Video tutorials

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
 
ribeirov1 profile image
Vitor Ribeiro

Image description
what this mean?

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')