How to Deploy a Django App and Postgres Database to Render

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in today's video I'm going to show you how to deploy a Django app to a service called render and along with a Django app I will deploy a database as well so you can see how to connect this postgresql database that I create to the Django app and then the Django app will be running on render so render is a service that is very easy to use it's very easy to deploy the things that they have here so I'm on their dashboard and you see you can deploy static sites web services so Django would be a web service background workers cron jobs postgresql and other things so the main thing you need to do after creating an account is to connect your GitHub account so here my GitHub login has been connected pretty printed and that will allow me to deploy the Django app easily so the app I'm going to deploy is this which is very simple this is just for demonstration purposes so this is a jingle app and it just allows me to add something I call Members so I'll just put my last name here Herbert and we see I've added Anthony pretty printed in Herbert to the database and the app looks like this so I have this render deploy app I have this example app within the Django project and in the views.pi I simply a query for all the members and then I add a member here by creating it and then just redirecting to the index so the app itself is very simple the reason why I made it this simple is so I can demonstrate the deployment process not so much building an app so if I go over here to settings.pi I can go down to the database part and this is the thing that I want to do first so right now I'm using this sqli database and it's generating the file here what I want to do is I want to use a postgresql database instead so I'll start on render by creating a postgresql database and connecting to it here on my local machine so let me go over to the render dashboard and I'll click new postgresql and then I'll just give it a name so uh let's call this pretty printed Django render and then I'll leave the database name to be randomly generated along with the user because I'm not too concerned about those I'll leave the region as origin the version is 15 that's the latest I'm not using datadog and I'm using the free plan here so I'll just click create database and in a moment it will create it so I'll wait for this to be done so now we see the status is changed to available the storage status is still pending but while we wait for that I can go to the connect information here and explain it so here I have two tabs one fit internal connection and one for external connection so if I'm connecting to the database from outside of render I want to use this external connection URL but if my app is hosted on render as a web service then I want to use the internal database URL so I'm going to copy the external database URL just copy that and we see the storage is ready now so I have one gigabyte of storage now go over to my code and inside of the code I'll just paste this as a comment so there are a couple ways you can do this you can set up everything here so you can change the engine to be postgresql you can change the name to be the database name and then you can put the username and password but I like to use a Django database URL so I will install that so pip install DJ J database URL and then I'll just go ahead and import that at the top so import DJ database URL and then I'll just use it down here so what I'm going to do is I'm going to overwrite the existing database settings output default here and I'll use that Django database url.parse and then I can pass in my URL here so I'll just cut and paste that into that okay so I'll remove this comment and I'll save this and now that I have a separate database what I'll do is I'll do python manage.pi migrate because I need to create the tables in the new database and we see I have an error here and this is simply because I don't have the postgres driver installed so I'll install that on my system it's cycle pg2 binary but on yours you might be able to get away with um cycle pg2 without the binary so I'll go ahead and install that and now I'll run migrate again and this time is running and it's not giving us any errors so while that runs I'm going to connect to my database directly with a tool called D Beaver so let me open that up and then what I can do is I can connect to it so this is just a tool to work with databases so I'll use postgresql as my database and then I can copy the URL in here and just take out the things that go into this certain field so that was the database name so I'll put it here pretty printed Django render the host is going to be everything after the AD Sign the username is going to be this user here so let me go ahead and take out the user so I can just grab after the r and then paste that into username and then after the colon but up into the at sign that's going to be the password and then that should be it so everything after the ads is the host I can test connection it looks like it's working so finish it appears here on the left so now I can open it and when I do open it I should be able to see the tables that were generated from my app so I can open up databases here schema public tables and then these are the tables and I have the example member table here so let's look at that because right now there should be no data in it so there's no data but now if I go to the running app so let me go ahead and start the app first so python managed up high run server and then I go over here when I reload the page we see there are no members so I'll go ahead and add Anthony as a member I'll add Herbert as a member and I'll add pretty printed as a member okay great so I added those three I can look at the database again and I'll just refresh this here and we see it has three entries Anthony Herbert and pretty print it in there so we know we are now connected to the render database and everything is working properly so as you can see it was very easy to set up the database and connect to it so later I'm going to come back and use this internal connection so I can connect to the database using this internal URL and the process is going to be pretty much the same I'm just switching out that URL so now let's think about deploying the Django app so I'll go back to the dashboard and I'll click on new here and I want to create a new web service so what it wants me to do here is to connect a repo that I have but I haven't created a repo for my project yet so let's go ahead and do that first so I can go here and I can do get a knit and I've turned this into a repo and what I want to do here is I'll skip the ignoring files but what I want to do in the settings.pi is I want to set certain things up to come from environment variables because I don't want some information to go in the repo so let's go through it I'm going to import OS so I can grab environment variables so one thing that needs to be an environment variable is debug so right now it's just true but I want this to be the value from the environment variable so what I can do is I can say something like OS dot Environ baguettes and I'm going to get a key called debug so I'm going to name the environment variable debug when I go to create it and it's going to return false by default and I want to check to see if this is equal to true so debug takes in a Boolean so I'm going to see if the debug value itself is a string that's true so if it's true it's going to equal true and actually to make this even better I can lower this and then but it's a lowercase shoe so if I put upper cases or all lowercase in the environment variable this will still work so as long as the string in the environment variable debug is true this will equal true if it's anything else then it's going to be false allowed hosts I can also create an environment variable for I'll do OS Environ dot gets and I want to get allowed posts so I'll just call everything the same as the setting in Django and what I'm going to do is I'm going to split it by spaces so this should be an S you'll see me use this when I go to the render dashboard and the idea here is I can have multiple hosts separated by spaces and then it's going to split them and return a list as you saw before it was a list and then after it will be a list and then finally the secret key I can do something similar this one is just straightforward I can do OS and viron git Secret okay so those are the three things that I wanted to make environment variables up here and then the last thing will be the actual database so here I can say like database URL equals OS Environ gets database URL and then I will remove the string and replace it with the variable so this is a really straightforward way of setting up the settings.pi there are many ways of setting this up so don't think that this is the best way this is just the way I wanted to do it for this video's purpose but there are different approaches to having settings up high you can have multiple settings.pi you can use like the Django Environ Library you can do multiple things but for the purposes of this video I just wanted to keep it like this so those are the things that I need to make environment variables so now I'll just go ahead and save everything to the repo so get at all git commit first commits and I probably should have added a git ignore because I have my virtual environment here so what I can do is I can actually create the git ignore and then I can do git remove hashed and then the EnV directory Cash Dash R the EnV directory and then in the git ignore I can just do slash gits and then I can do git add all again get commits I removed EnV okay so now the EnV directory is no longer my git repos so it shouldn't take so long to commit next time there are other things that I could ignore like uh Pi cash and uh the sqlite file but I'm just going to leave that like this and the 10 000 here is no longer correct because I've already committed the repo okay so now I want to go to GitHub and I want to create a repository so I'll call this render Django example and then I'll set this to private and I'll create the repo and it's going to give me the get remote ad origin link down here so I'll just copy that and I'll paste it in here so git remote ad origin and then I can do git push origin Master that's going to push all my code over to GitHub so that's done I can refresh now we see this here the EnV directory is gone because I removed it so now let's go and do one more thing because this is a python app I need a requirements.txt so I have the virtual environment working on my local machine but when render goes to deploy this it needs to install all the same libraries so what I can do as I can do uh pip freeze and then put this in requirements.txt and then I can just add that to my commits edit requirements and then just do get push origin Master again and now we see when I refresh the requirements.txt is here so anytime you update the requirements.txt you'll need to push it to your repo and then redeploy the service to render because it needs to install the new libraries so that's it for the GitHub site now let me just refresh this page so my new repo loads and when it does I can just find it and hit connect so there it is I'll hit connect and then I'll give it a name so Django render example app uh the region will be Oregon West's again the branch will be master so if you had a different branch name you can use that but in my case it's just Master the root directory is not relevant for this uh the runtime will be python3 so this is a python project and then the build command is going to be pip install requirements.txt so that's why I just generated the requirements.txt and now we see the Star Command which is going to actually run the app so I should install G unicorn and then I can do pip freeze requirements.txt again and then I can add this to the repo and just push it and the idea here is I can run this wsgi so what I can do is I can say G unicorn and then the name of my app so the name of my app is render deploy render deploy Dot wsgi and if there are any problems with this we'll see it when it goes to deploy so the instance type is free once again but of course you're going to have another one if you decide to pay and then I'll create the web service so that's all I need for right now and then it's going to try to deploy it so there are going to be problems with the deploy and I'll fix them as they come up to kind of show you how other things work here okay so the first error we get is a can't find Django 4.2.2 so at the time of recording this video that's the version of Django that I'm using because we see here in the requirements.txt it displays as 4.2.2 so the way to fix this is I can just downgrade so let me downgrade to four 0.0 and once again I can just commit everything so because I modify requirements.txt directly I can just commit without having to do Pit freeze so I get commit and then change Django version so git push origin master so this is something you probably won't have to do but in my case the versions are just out of date for from wherever it's pulling the versions so I'll go back here and now I can do uh manual deploy and then deploy latest commit so I can start the process again and once again there are going to be problems but like I said we can deal with them as they come up and it looks like it doesn't even take 4.0 so the latest version here is 3.2.19 so I'll just change it again so the reason why I show these in the video these um failures is because oftentimes when people go to deploy apps it doesn't work exactly perfectly the first time around so I just want to go through the process of debugging everything so you can see what's happening so I updated Django version again so there are very little differences between Django 3 and Django 4 that's why I can get away with doing something like this but but normally you want to keep the version as close to the latest as possible so I'll go back here and I'll once again uh do the manual deploy and let's see what happens this time because there are going to be more issues okay so we see that the build was successful and now it's deploying so I don't expect the deploy itself to work but we'll see what happens when it finishes and just keep in mind that when you're using the free plan this is going to be a bit slow because if you want the fastest you need to pay for it okay so here I see a loud host um is missing and the reason is is because I haven't specified that in my environment so what I can do is I can go over to the environment here and I want to specify all the environment variables that I have so I can add them so one will be secret key and I can just generate a secret key here right so it doesn't really matter what the secret key is it just needs to be secret I'll add another one this will be allowed hosts and the value for this one should be a string with spaces between the hosts so I'll just do that in localhost really you don't need localhost but just to demonstrate that this is going to be a string with two things I'll do it like that so I can add another one and I need the debug so I want the debug here to be false and I'll add one more for this one I want the database URL and now we can go back to our database so I'll just open a new tab here and I'll go over to the database that I have so uh the Django render database and I want to get the internal connection URL this time copy that and just paste that into the environment variable value so now I'll save this and when you save environment variables it will start a deploy automatically for you so let's just wait and see what happens here okay so it looks like everything is live so let's click on the link here and let's see if we can use the application and we see here it's now bad requests and because we see bad requests here we know something isn't working properly but we don't know exactly what the issue is so what we can do is we can go to the environment variable here we can edit the debug and we can put that to true so this will just be a momentary change so we can debug this and figure out what's going on so the app is going to deploy itself again okay so now that this is running we see a more detailed error message so we see Django render example app on render.com is an invalid host and the reason is is because we put too much in the environment variable so if I go to the environment variable here I have the HTTP s colon slash slash so let me remove that save it and then let the app deploy again and hopefully this is the last issue we have with this deploy process okay so it's deployed again now I can refresh here and now we see the name so everything is working properly with the app because it's using the same database as before we already have data so I can add a new member here and it will get saved and new member appears there so everything is working what I can do is I can change debug back to false and I just wanted to show you one more thing I can go down to settings and they have the option of having a custom domain I don't have any domains available but using a custom domain is pretty simple so you can just follow the instructions here and you'll have your own domain for your app instead of using the URL that they have here like this ugly one on render.com so that's it for this video that's all I want to show you I wanted to show you the deploy process and some of the common issues that you'll run into when you go to deploy a Django app or really any other app but this video was specifically Django so if you have any questions about anything that I've done this video feel free to leave a comment down below if you like this video please give me a thumbs up and if you are not yet a subscriber please subscribe to my channel so thank you for watching and I will talk to you next time
Info
Channel: Pretty Printed
Views: 19,516
Rating: undefined out of 5
Keywords: render, postgresql, deployment, django
Id: wczWm8j4v9w
Channel Id: undefined
Length: 20min 14sec (1214 seconds)
Published: Thu Jun 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.