Deploy Django App To Railway | Alternative To Heroku | 2023
Ғылым және технология
In this video, I will tech you how to deploy a Django project to railway. Railway is an alternative to Heroku
Diango documentation: docs.djangoproject.com/en/4.1/
my instagram: the_proton_guy
my twitter: AyodejiAdesola_
#railway #djangorailway #django
Пікірлер: 110
HOW TO SOLVE STATIC FILES NOT SHOWING: Step 1:run pip install whitenoise step 2: run pip freeze > requirements.txt step 3: In middleware in your settings.py file, add this line below the first middle: "whitenoise.middleware.WhiteNoiseMiddleware", step 4: Configure static files. make sure you have something like this: STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / "staticfiles" STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] step 5: Add this line to your settings.py file: STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" step 6: run python manage.py collectstatic step 7: push to github and try to redeploy That should solve the issue
@polynomen
Жыл бұрын
it throws error 500 in production
@the_proton_guy
Жыл бұрын
Can you check the logs? To see of there's any error
@polynomen
Жыл бұрын
@@the_proton_guy I fix the 500 error by putting this object as in the white noise docs instead that storage: STORAGES = { "staticfiles": { "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", }, } anyway it doesn't show static files but only the components without styles. To activate styling you have to use the debug to True, but it s not recommended to use debug in production !
This was really helpful, thanks.
@the_proton_guy
Жыл бұрын
You are welcome 🥳
Thanks for the tutorial! it really clear and helpful
@the_proton_guy
Жыл бұрын
You are welcome 🥳🥳
Really appreciate your effort,thanks very much 🙏
@the_proton_guy
10 ай бұрын
You are welcome 🥳
Is your app correctly listening to $PORT. How to solve this? I even added PORT with the value of 4000 or 8000 it still won't work.
What is your 'Start Command from Deploy' in settings on railway?
@the_proton_guy
Жыл бұрын
gunicorn ProjectName.wsgi --log-file -
How to solve the CSRF issues? I cannot now access to the admin page. Error: Forbiden(403) CSRF verification failed. Request aborted.
@the_proton_guy
Жыл бұрын
You need to checkout csrf trusted origins. Cjeck this stack overflow question stackoverflow.com/questions/38841109/csrf-validation-does-not-work-on-django-using-https
does it support python 3.11 and 3.11.1, and how to upload it without error docker build failed,nixpacks build failed
@the_proton_guy
Жыл бұрын
Yes I think it does. Let me look jnto the error and see what fix I find
Hello,I have this problem Nixpacks build failed Error: Error reading requirements.txt Caused by: stream did not contain valid UTF-8
@enderman23456
Жыл бұрын
I have the same problem
@the_proton_guy
Жыл бұрын
Can you kindly shed more light on this error
@kishanKumar-co7wb
Жыл бұрын
@@the_proton_guy Getting error can you help me?? Nixpacks build failed Nixpacks was unable to generate a build plan for this app.
Best video
@the_proton_guy
Жыл бұрын
Thank youuu
Hello am getting not found after a successful build. How do I solve it
@the_proton_guy
Жыл бұрын
A 404 error?
Hey, I am getting this error can you help me Building wheels for collected packages: twisted-iocpsupport #11 22.64 Building wheel for twisted-iocpsupport (pyproject.toml): started #11 23.27 Building wheel for twisted-iocpsupport (pyproject.toml): finished with status 'error' #11 23.28 error: subprocess-exited-with-error #11 23.28 #11 23.28 × Building wheel for twisted-iocpsupport (pyproject.toml) did not run successfully. #11 23.28 │ exit code: 1 #11 23.28 ╰─> [13 lines of output] #11 23.28 running bdist_wheel #11 23.28 running build #11 23.28 running build_ext #11 23.28 building 'twisted_iocpsupport.iocpsupport' extension #11 23.28 creating build #11 23.28 creating build/temp.linux-x86_64-cpython-310 #11 23.28 creating build/temp.linux-x86_64-cpython-310/twisted_iocpsupport #11 23.28 gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -I/nix/store/vamwsqfpvgfwnc7ksa8k4aa52my5r1bq-libxcrypt-4.4.33/include -fPIC -Itwisted_iocpsupport -I/opt/venv/include -I/nix/store/al6g1zbk8li6p8mcyp0h60d08jaahf8c-python3-3.10.9/include/python3.10 -c twisted_iocpsupport/iocpsupport.c -o build/temp.linux-x86_64-cpython-310/twisted_iocpsupport/iocpsupport.o #11 23.28 twisted_iocpsupport/iocpsupport.c:767:10: fatal error: io.h: No such file or directory #11 23.28 767 | #include "io.h" #11 23.28 | ^~~~~~
Great video. But it does not describe what to do with environment variables.Where did you place your secret key? Railway? Github?
@polynomen
Жыл бұрын
I put them into railway and it goes
thanks for your video, I have troubles with databases, deploy fails at Collecting mysqlclient==2.1.1 and then at Collecting psycopg for postgress database, would you help to understand how to fix that dependencies
@the_proton_guy
Жыл бұрын
Hi, okay. Why do you have mysqlclient and a Postgresql database? A fix for the psycopg2 is: Instead of using psycopg2, replace it with psycopg2-binary. So pip install psycopg2-binary. After that, run pip freeze > requirements.txt to save the new library to the file Then, go to the requirements.txt file and locate where psycopg2 is and remove it. Push to github and your railway app should automatically start deployment
@davidmejia9601
Жыл бұрын
@@the_proton_guy thanks, I tested first with mysql but didn't work, for posgress I tested with psycopg2-binary but the error continues I was checking in some post and they says that is a problem of the SO disfribution, maybe I will try with aws maybe that can help me to deploy, heroku is not more free
@the_proton_guy
Жыл бұрын
Did you remove psycopg2 from the requirements.txt file?
Hello please I'm having an error "Error Docker build" please help me fix that thank you
@the_proton_guy
Жыл бұрын
Hey, can you check the deploy logs to see what the error is?
Could someone help me clarify a question? I've successfully deployed my Django App on Railway using SQLite. Now, let's say a user makes a post request that updates the database. Will these changes be reflected in my GitHub repository? In other words, if I make a new push to my GitHub repo and Railway re-deploys, will it recognize that the existing database file is already up to date and avoid replacing it with the outdated one from GitHub?
@the_proton_guy
Жыл бұрын
No, the data will not be saved on github. If railway redeploys, it will overwrite the existing database with the old one from github. If you wish to remove this issue, connect to an external database. You can create and connect to a postgresql database on Railway
@huzaifafaheem6382
Жыл бұрын
@@the_proton_guy I see, Thank you very much! Can I somehow download that database on railway that was assigned to me so that I can manually push it to github?
@the_proton_guy
Жыл бұрын
Hmm, I am not so sure about that, but for future purposes, if you want to export your database for backup purposes, look into ghe django import export library
Hello , how do I add superuser to my django app on heroku
@the_proton_guy
Жыл бұрын
For this, you will need to go to your app on heroku. Next, you'll need to click the button labelled more on the top right of your screen. Next select run console. After that, a window should pop up where you can run commands. So just run python manage.py createsuperuser and all should work
@lucasobwaku9080
Жыл бұрын
I never deployed the project to heroku . I only deployed it railways. Does it mean I have to again deploy it to heroku for it to create superuser
@the_proton_guy
Жыл бұрын
No, what you need to do is add a database to your project on railway. Luckily, they have a PostgresSQL database you can use as an add-on to your project. this link below will take you to an article that will teach you how to do this: dev.to/mr_destructive/django-postgresql-deployment-on-railway-app-d54 After that, you will be able to run createsuperuser and have the user saved in the PostgresSQL database
Hello sir, the build works but I am getting an error ModuleNotFoundError: No module named 'tdl' when I deploy. kindly advise, thanks
@the_proton_guy
Жыл бұрын
Hey, in your procfile, replace tdl with your django project name and redeploy
Which database you used? The database is giving me a lot of issue
@the_proton_guy
Жыл бұрын
Sqlite db
how to save user upload files in railway?
@the_proton_guy
Жыл бұрын
You would need a database for that
Error: No start command could be found it's showing me this error
@the_proton_guy
Жыл бұрын
Can you give me more details ? Like maybe copy and past the errors so I can see
how to i access the python installed for the application i dont see any terminal option in the site and i did install railway by scoop and using shell but i cant acess the python installed for the application pls help
@the_proton_guy
Жыл бұрын
Umm, are you trying to access your app on Railway through your terminal?
@baitman548
Жыл бұрын
Yep I would like to have direct access to the installed packages in the env
@the_proton_guy
Жыл бұрын
Hi, sorry for replying late. You can access the cli through railway cli. Visit the link below to learn more about it: docs.railway.app/develop/cli
@THE PROTON GUY I'm stuck at one error : Nixpacks build failed Error: stream did not contain valid UTF-8 please reply me asap
@the_proton_guy
Жыл бұрын
Can I see your repository
@shakeelriaz5191
Жыл бұрын
@@the_proton_guy Hey, I solved that error by saving the requirement.txt file and shifting it into UTF-8 from UTF-16. but now I'm stuck on modules. when installing the modules, an error comes that "Could not find a version that satisfies the requirement pywin32==304" Docker build failed
@shakeelriaz5191
Жыл бұрын
@@the_proton_guy Is there any way to deploy Django App on Railway Manually??
@the_proton_guy
Жыл бұрын
"Pywin32==304". Isn't it suposed to be something like pywin32==3.0.4 Check your requirements.txt and if pywin32 is there, make a change to 3.0.4 and try again to see if that fixes the error
How can I change the Branch ? by default its using the master branch, I want to change it.
@the_proton_guy
Жыл бұрын
Hii, this can be done from the settings tab of your project in railway
@tanimsk
Жыл бұрын
Thanks!
@the_proton_guy
Жыл бұрын
You are welcome!
Thanks❤
@the_proton_guy
Жыл бұрын
You are welcome bro
my deployment is stuck in building ...does anyone know why !! Irealy appreciate the help
@the_proton_guy
Жыл бұрын
Do you have any error in the build logs?
@Best_Real_Experience
Жыл бұрын
@@the_proton_guy actually it's working now ..alhamdulillah......thank you sir for the fast reply ❤ .you got new subscriber
@the_proton_guy
Жыл бұрын
You are welcome man 🥳🥳
thank you for the tuto i only have one question what about the media file how to configure and server them via railway ty
@the_proton_guy
10 ай бұрын
Hello, you will need to find a place to store your media files, like an s3 bucket or cdn like cloudinary, as it is best practice
@devilmeliodas9091
10 ай бұрын
@@the_proton_guy thannk you
@the_proton_guy
10 ай бұрын
Happy to help 🥳
@devilmeliodas9091
10 ай бұрын
@@the_proton_guy im just learning so do you have any free suggestions ?
@the_proton_guy
10 ай бұрын
@devilmeliodas9091 yes. I would highly recommend cloudinary, although you can also look into Amazon s3 buckets
Its the same thing as heroku or is it better than heroku?
@the_proton_guy
Жыл бұрын
I wouldn't say better, but it's good
how to solve this error: Is your app listening correctly on $PORT?
@the_proton_guy
Жыл бұрын
Ummmm, did you configure allowed hosts well
@hiaguinhospencer8778
Жыл бұрын
@@the_proton_guy where do I configure, in VS CODE or in Railway?
@the_proton_guy
Жыл бұрын
Vs code, in your settings.py
what can i do if i have postgreSQL database with my project
@the_proton_guy
Жыл бұрын
Is your postgresql online?
@akrambengueddoudj2513
Жыл бұрын
@@the_proton_guy no, it's local.
@the_proton_guy
Жыл бұрын
You can create a postgress database on Railway as a project and connect to it from your django project. That way, your Web app and database are in the same place
@akrambengueddoudj2513
Жыл бұрын
@@the_proton_guy thank you
@the_proton_guy
Жыл бұрын
You are welcome
Hey, Firts of all great video! I have an error that I can not fix because I didnt find any information about it, I'll paste it here: ============== Using Nixpacks ============== Nixpacks build failed Error: stream did not contain valid UTF-8 Maybe You can help me with that!
@the_proton_guy
Жыл бұрын
Ok, I'll look into it
after deploy I got server error 500
@the_proton_guy
Жыл бұрын
Um, okay. Can you check the deploy logs to see what the error is?
@f-s_interpreter
Жыл бұрын
@@the_proton_guy Are u genius? I saw a guy asked you before , u told him just check, ant it is worked. Now I made liitle change(exactle removed psycop2 and used binary one) and it works. THANK YOUUUUUUUUUUUUUU
@the_proton_guy
Жыл бұрын
You are welcomeeee😹😹a sub to the channel will be helpfulllll🥺🥺
Hello, I'm from Brazil. Thanks for the video. In deploy I have the following error message: do you know what it could be? usage: gunicorn [OPTIONS] [APP_MODULE] gunicorn: error: argument --error-logfile/--log-file: expected one argument
@the_proton_guy
Жыл бұрын
Hiii, are you sure you configure your Procfile properly
Hello, how do you do? I am getting the following error when deploying my Django project: Dockerfile:21 ------------------- 19 | RUN printf ' PATH=/opt/venv/bin:$PATH' >> /root/.profile 20 | COPY . /app/. 21 | >>> RUN --mount=type=cache,id=s/12ad632d-3245-4503-b551-fa89e82b6a0e-/root/cache/pip,target=/root/.cache/pip python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt 22 | 23 | ------------------- ERROR: failed to solve: process "/bin/bash -ol pipefail -c python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt" did not complete successfully: exit code: 1 Error: Docker build failed Please help
@the_proton_guy
Жыл бұрын
Hey. Can you check the logs to see if any library from the requirements file could not install properly
@techwithdenzel
Жыл бұрын
@@the_proton_guy thank you so much for responding. Which logs, the deploy?
@the_proton_guy
Жыл бұрын
Yes, the deploy logs
@techwithdenzel
Жыл бұрын
@@the_proton_guy Here's the full log: #11 11.83 creating build/lib.linux-x86_64-cpython-311/cairo #11 11.83 copying cairo/__init__.py -> build/lib.linux-x86_64-cpython-311/cairo #11 11.83 copying cairo/__init__.pyi -> build/lib.linux-x86_64-cpython-311/cairo #11 11.83 copying cairo/py.typed -> build/lib.linux-x86_64-cpython-311/cairo #11 11.83 running build_ext #11 11.83 Package cairo was not found in the pkg-config search path. #11 11.83 Perhaps you should add the directory containing `cairo.pc' #11 11.83 to the PKG_CONFIG_PATH environment variable #11 11.83 No package 'cairo' found #11 11.83 Command '['pkg-config', '--print-errors', '--exists', 'cairo >= 1.15.10']' returned non-zero exit status 1. #11 11.83 [end of output] #11 11.83 #11 11.83 note: This error originates from a subprocess, and is likely not a problem with pip. #11 11.83 ERROR: Failed building wheel for pycairo #11 11.83 Failed to build pycairo #11 11.83 ERROR: Could not build wheels for pycairo, which is required to install pyproject.toml-based projects #11 11.85 #11 11.85 [notice] A new release of pip available: 22.3.1 -> 23.1.2 #11 11.85 [notice] To update, run: pip install --upgrade pip #11 ERROR: process "/bin/bash -ol pipefail -c python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt" did not complete successfully: exit code: 1 ----- > [stage-0 7/8] RUN --mount=type=cache,id=s/12ad632d-3245-4503-b551-fa89e82b6a0e-/root/cache/pip,target=/root/.cache/pip python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt: #11 11.83 Command '['pkg-config', '--print-errors', '--exists', 'cairo >= 1.15.10']' returned non-zero exit status 1. #11 11.83 [end of output] #11 11.83 #11 11.83 note: This error originates from a subprocess, and is likely not a problem with pip. #11 11.83 ERROR: Failed building wheel for pycairo #11 11.83 Failed to build pycairo #11 11.83 ERROR: Could not build wheels for pycairo, which is required to install pyproject.toml-based projects #11 11.85 #11 11.85 [notice] A new release of pip available: 22.3.1 -> 23.1.2 #11 11.85 [notice] To update, run: pip install --upgrade pip ----- Dockerfile:21 ------------------- 19 | RUN printf ' PATH=/opt/venv/bin:$PATH' >> /root/.profile 20 | COPY . /app/. 21 | >>> RUN --mount=type=cache,id=s/12ad632d-3245-4503-b551-fa89e82b6a0e-/root/cache/pip,target=/root/.cache/pip python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt 22 | 23 | ------------------- ERROR: failed to solve: process "/bin/bash -ol pipefail -c python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt" did not complete successfully: exit code: 1 Error: Docker build failed
@the_proton_guy
Жыл бұрын
hi, i am sorry. just seeing this. The error seems to be pointing to a library or package called cairo
Hello how are you? thanks a lot for your video. I have multiples troubles while deploy to railway. The first is that I have tailwind installed on my django app and it dont load when I deploy on railway and the other problem is that I have this on my deploy logs: [2022-09-21 08:05:02 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:291) [2022-09-21 08:05:02 +0000] [291] [INFO] Worker exiting (pid: 291) [2022-09-21 08:05:02 +0000] [292] [INFO] Booting worker with pid: 292 /opt/venv/lib/python3.10/site-packages/environ/environ.py:648: UserWarning: Error reading /app/.env - if you're not configuring your environment separately, check this. warnings.warn(
@the_proton_guy
Жыл бұрын
Hiiii, are you sure you configured your static files properly, and did you use a virtual environment?
@valentinfigueiredo
Жыл бұрын
@@the_proton_guy images are loaded but not the css, I have : Not Found: /static/css/dist/styles.css in my deploy logs. how can i check on railway cli if i use virtual env? because on local all is working properly with my virtucal env
@the_proton_guy
Жыл бұрын
That's unusual, I don't think the railway has a cli you can interact with. Let me search for an answer and I'll get back. to you
@valentinfigueiredo
Жыл бұрын
@@the_proton_guy ok thanks a lot
@the_proton_guy
Жыл бұрын
Ok so i found a solution. Step 1: pip install white noise step 2: pip freeze > requirements.txt step 3: In middleware in your settings.py file, add this line below the first middle: "whitenoise.middleware.WhiteNoiseMiddleware", step 4: Configure static files. make sure you have something like this: STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / "staticfiles" STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] step 5: Add this line to your settings.py file: STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" step 6: run python manage.py collect static step 7: push to github and try to redeploy That should solve the issue