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
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,
click on your project go to the settings
tab
under domain click generate domain
a customized link to your deployed app is been created you can visit it by clicking on it.
VOILA!!!
Your project has been deployed.
CONGRATULATIONS!!!
Top comments (52)
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.
What can I do to fix this problem on railway?
Need to set in settings:
Hi, How to know my "your-base-domain"
"your-base-domain" is your project name
You base domain is the root address of your app. In railway you find it in your domains setting for you django application.
I have the same problem! What did you do to solve it? thanks
I didn't be able to solve it.
Until now I am still looking for a solution
Thanks for the feedback. If I find the problem, I'll post the solution.
Maybe we should make a short call for verification. We need your help really
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.
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 :)
I have a error about utf-8
Error: stream did not contain valid UTF-8
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
How can I do that?
I have this error too. how did you solve it
Use whitenoise
I had this error too, change the file encoding for requirements.txt to utf-8
This work for me, thank you so much
can you correct the typo: pip install gunicorn
Fixed. Thank you for your review
Thank you so much! Really good article
seems it works only if debug=1, with debug=0 it doesn't see static files
Aitec/aitx (gatx)
I need a help. I am meeting an issue with static files.
Here is my settings.py
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.
Everything is ok now.
The whitenoise was missing. I added the whitenoise to the middleware and everything is ok now.
Thanks
Thanks for your feedback
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')
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
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
Yeah, my requirements file is requirements.txt but I'm still getting the same error
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.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