How to quickly install and set up Django with a PostgreSQL database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's john welcome back uh today i thought i'd go over the setup for a brand new django app in case you're wanting to work on a side project or you're familiar with django and just want a quick and easy way to get set up with a postgresql database i recently built an app called nif gifts for my family and i during the christmas holidays because before we had this confusing system of google spreadsheets and basically this new app allows you to add a wish list for yourself to join a group to create different events um for each holiday or birthday or whatever you can create a list of all the gifts that you want other people can add to it but you'll never know what they add it'll just show up as like a secret gift and it was kind of a fun side project so if you're wanting to just build something useful and you want to use django to do it and maybe you just want to learn django this is a perfect way to just get started quick and easy and that way you can get started creating the app that you want to do so we're going to go through the setup today in authentication and i'm getting connected to postgresql database and if it sounds interesting to you if you want to go through the different sections beyond just setup of how i built that app or how to build a django app for yourself just let me know in the comments so that way i'll know if that's a valuable use of your time for upcoming videos okay so let's get started first thing we're going to do is we're going to open a new terminal and we're going to set up a virtual environment um but before before we do that we're gonna need python so make sure that you have python installed i'm gonna check uh versions just to see what we have 383 that's fine and then we're going to create a virtual environment and this is best practices for django and a virtual environment allows you to have an isolated environment for each individual project that you create so it'll have its own packages with their own versions and you won't have to make sure that um you know you have something installed globally that that works for every project and you're trying to sort out all the compatibility issues you just have a clean isolated environment for every project if you've installed python you have something called vm venv that'll allow you to create a virtual environment pretty easily so let's go ahead and create the first environment so i'm going to just use this command vmv i'm going to go into my home directory virtual ms and then i'm gonna name it so this is the location where the environments can be located i'm just gonna call it um let's just call it a gift inv like that okay so we've created the environment so now to actually use the environment you have to activate it and there's a command for that and all you do is you say source and then you go to where the environment is located in this case gift ends and then you go to bin activate and if you see now we have we're in the new virtual environment and then when you want to get out of it you can just click you can just type in deactivate enter and it'll it'll exit the virtual environment okay so next step of setup is we're going to install and set up postgresql i like postgresql it's just a simple and easy way to to create a database i'm familiar with it i've used it in the past so there's a little bit less friction for me using it and there's generally some decent documentation uh with postgresql in in django so the first step is i'm going to use the postgres.app to install postgres we have to install um uh postgresql first before we use it um and so i'd like to there's there's a few different ways you can do it as as mentioned on on the website but i like using something called postgrestapp.com so i'm going to open up a new chrome window and pull it onto the screen and we're going to go to postgresapp.com it'll give you these instructions in case you want to look at it on the website but i'm going to go through them here um first thing we're going to do is we're going to download the app i'm just going to download the latest version okay i'm going to open it all right and it just showed up on my other screen you have to drag it into the applications okay i'm going to open the applications and here it is so let's open postgres yep i'm sure okay we have to initialize it okay so now we're running the server and um we've initialized it and now the next thing i'm going to configure the path so that we can actually run the psql command as a client to interact with the database so they give you this command on the website here and i think it's under documentation installation and then they give you a handy little script here so you don't have to remember it so i'm going to do this so now i can just use psql if i want to interact with the database uh oh i have to restart the terminal for that to work so we're gonna use this in in just a little bit so um okay so once we've installed postgresql um we're gonna install a package um called uh psycopg2 and what this is is it's basically a postgresql adapter for python so that postgresql can work with the python programming language and it's basically a wrapper over the postgresql library so uh we're gonna install it using pip which should have come with your with your uh uh python installation so we're just gonna run um pip3 install and i always spell this wrong psycho pg2 okay oh it's missing pg config and once again we're gonna have to open a new terminal window to resolve this air in case you get this so let me open a new terminal window that should resolve our issue okay so um let's try this one more time let's we have to go back into our virtual environment right so source virtual ends um and then we call it gift and right yep then activate okay so we're in here um and now we should be able to install this pg2 and it should work so we have that installed and then now we should be able to use psql and interact with the database right here okay so oops oh i forget i always forget this command exit um okay so now that we have postgresql installed we we definitely want to install django if we're going to create a django app right so the easiest way to do that is we can run python 3 in pip install django okay and then let's make sure that it actually installed so we're just going to run python 3m django version 314. great okay so we've installed django we've installed postgresql and now what we want to do is create our project that we're actually working with so i'm going to be i usually all my projects usually live in my home directory under a project subdirectory so i'm going to cd to that as you can see i have some other projects in here that i've been working on lately but i'm going to go ahead and create a new project so i'm going to go django admin start project gift demo okay and then let's look at this and it looks like a gift demo is right there so i'm going to cd into that and then open it in visual studio code and we can go ahead and expand this all right um so you can see that it set up some some files by default so manage.py this is an important one it's a command line utility and this helps you do things like make migrations which alters like basically the data in your database as well as any reflects any changes to your models which we're going to go over what models are and how to use them but manage.py is how you start your server as well it does some important things urls.py that's a uark url root configuration so that's how django knows uh when when somebody you know enters the url into the browser and hits enter on your app um this is how django knows where to route those requests um to perform the right functionality and to get to the right place and then basically this wsgi and asgi those relate to web servers which we're not going to go into that right now we're just going to be in a development environment but if you want to set up a web server when you deploy you might be dealing with those with those files so um okay cool and so first let's just make sure that we actually install this correctly so to do that once again we're going to have to enter into our virtual environment um i think this is oh django devs did i answer the wrong one so in case you're in the wrong virtual environment hit deactivate and then uh let's activate the correct one so source virtual ends gift m and then we're going to go to bin activate and now we're in the correct virtual environment cool so now that we've done that we just want to run python3 manage.py run server and this should start the django server you will see this default message every time 18 unapplied migrations there's some default installed apps and in if you look here admin auth content types and sessions we're going to migrate later and i'll go through that so don't worry about that you'll still be able to test whether your app is up and running so to do that i'm going to open up the new new window over here well actually let's see if it'll just if we do command click show up on my second screen see i'm gonna drag it over here i'm gonna minimize this first here we go so if you look here this install works successfully congratulations this should be the default landing page once you actually set up a new django project and get the server running so that means that things are working as planned for now so that's good so let's continue so once you know that your development server is working okay let's go over projects versus app we're about to create a new app but first let's understand what these terminology with this terminology means so a django project is you can think of it in terms of a website would be one project and a project can consist of multiple apps you can have one app for authentication you can have one for for instance we're creating kind of like a gift exchange app and it represents kind of concrete segments of components of functionality in our case there's not much difference between the project and the app because we only have one app but if you're creating a bigger project um you're probably going to have multiple apps i'm going to keep this tutorial very simple just to make it easy to understand in case you're not familiar with django so the way that we create the gift app is uh let's stop the server we don't need that right now uh we're gonna run python3 manage.py start app and then we're gonna put the name whatever you wanna name your app i'm going to call it gifs okay and we should see here on the left hand side that it creates a bunch of stuff for us so this app i'm going to put it at the top level so it's it's a top level module not a sub module um just to make things simple and easy for the sake of this tutorial and as you can see it has migrations which are going to keep track of any migrations that we do and then a few other files that we may get into in this tutorial or in the next tutorials but um let's let's keep moving um in order to kind of go step by step here um so the first thing that we want to do is set up a view um now a view is basically a python function and it accepts a request an http request and it returns an http response at the basic level that's all a view is it's a function python function that takes an http http request returns an http response it can also take some optional parameters but this is how we deal with the request response cycle so those live we already have a views.py file it created for us and we're going to create the most simple view that we can this is called index an index usually you can think of it as kind of like the main landing page for your website generally speaking it's it's a place where you can kind of you know either list out it lists out what you need to list out as far as like the root navigation to get anywhere else but it's generally called index and we're going to pass in a request in this you have to do this for every django view every jenga view takes a request and we're going to import something from django called from django http called http response that'll kind of help us return an http response conveniently and we're going to say return http response hey world what up hello world's just so cliche don't you agree so this is the most simple django view that we can possibly do all right but before this does anything we have to know how to how do we get to this view if some like how are you going to navigate to this view in a browser and that's where that url configuration comes in so at the level of our actual app gifs we need to create a file doesn't come with it so we just call this urls.py and this is the url configuration for our actual app and this is how this is the path that django uses to know which view to actually call so we're going to use a few things here we're going to import from django.urls we're going to import path so the way we have a convenient way to specify url paths and then we're going to also import views so that way we can access the views and know how you know which views we can route to from the urls we're also going to create a namespace called gifs and you can imagine if you have multiple apps for your project you might not want to have unique nicknames for all your you know shortcuts for all your url paths you might have um you might have like slash edit for a gift uh and a group or or a gift and then some other uh you know object or model in a different app you want you don't want to mix and you don't want to mix up and duplicate url shortcuts and names and names so each url path name is going to be unique for a given namespace it's going to have its own meaning there so django's going to look for a variable called url patterns why am i having so many typos today url patterns and it's going to go in order down the url patterns list here to see each path and try to find a pattern match so for our case we just want to start out with a simple index so we're going to go to i'm going to use empty quotes to specify that if you just go to the straight up root domain here at http 127 you know blah blah 8000 we don't want to have to enter any backslashes or anything so i'm just going to make this kind of the main root path i'm going to say views.index because that's the the view that we're going to use that we just defined in in our views and then specify the name and we can use this as kind of shorthand when we're referring to this path so slash index okay so this is at the app level but we need to make sure to tell our project where to find the url for a given app generally speaking you can do things like like if you wanted to specify a url path like gifs slash so that way every url path is is clearly a part of the gifs app you can do that but in our case we only have one app so i'm not going to do that i'm actually just going to do a simpler version here i still want to be able to just enter in the normal url and then go to right where we want to go so i'm going to say path include and this is just a way to include all of the urls for our gift apps so this points to the app's url routings from the root url configuration of the project so this basically takes whatever url url routing comes after this prefix and it adds it on to route it to the view this goes in order so i'm just going to put this first okay so we have kind of a basic url configuration set up and we need to actually import include here because we i forgot to do that um so i'm going to do that right here so i'm going to do that right here and now we'll save it and everything looks good so now let's test to see that this actually works um so let's start at the server one more time should be up here okay all right let's refresh hey world what up okay cool so that's our first view working um just a simple http response um but um obviously we're gonna be building that up to be more interesting okay so we have we have a basic view running and now that we have that running let's actually set up the the postgresql database in django uh we've already installed postgresql and done the the general kind of overhead to get that set up in our virtual environment but now we have to make sure that django recognizes it and can connect to that database um so the first place that we're going to go we're going to go to settings.py and we're going to go find the database variable here okay and it defaults to sqlite 3 which is fine if you're just wanting just a quick proof of concept but generally speaking i find it easier to just get what i think of as a real database because i think this is just a file here but i want to just connect to postgresql up front so i don't have to kind of go through the pain of changing anything when i want to get closer to production so the first step we're going to do is instead of sqlite3 we're going to call it postgresql and then the name we are going to rename this and let's just call it giftdb we're also going to include a user which i will say jsteen and then a password and i'm just going to use a random string now it's important to note if you're in production you're going to have to secure these and make sure that you encapsulate these in a different way because it's not secure to just have these exposed in your settings.py so generally i'll either create a separate environment file like dot emv and set these as environment variables and then you can use like os dot gitm but we're not going to go into that in this tutorial but if you're interested in that let me know in the comments and i'll do that in a future tutorial so we're just going to have the basic setup for now and then we actually have to create the database before we connect to it so i'm going to do that we're going to enter into the postgresql client and then we're just going to say oh froze for a little bit create database gift db okay create database giftdb hit enter oh i always forget the semicolon for some reason so that created our database and then let's also create the user so we're going to say create user jsteen i'm going to say a super user just because i want to have any kind of permission so i want to have kind of ultimate authority here so i'm going to create myself as a super user and then i want to also add the password here so i'm going to say alter user jsteen with password and then i'm just going to copy in this password all right so now we have a database a user with a password and that is great okay so now that we have the database set up if you see here django has already specified some installed apps in each of these apps most of these are going to require tables to be included in the database to to store data related to to these installed apps and so like auth we have to find a way to keep track of like authenticated users and so on and so forth so the initial migrations are going to have to create tables for these so when i'm about to use a command to migrate and what migrate's going to do is sync basically sync your database to what your models look like so it's going to reflect any changes in how you want the data to be organized and how to be structured in your database so i'm going to exit out of the client here and then to migrate what you're going to do is just run the command python manage.py migrate okay so you can see it applied all these changes and now these installed apps should be set up and if we go into our database so if we go into the client once again gifd and then we look at our tables you'll see that we have a bunch of tables here we have auth group a bunch of auth tables and then tables related to session migrations and so on okay so that's that's kind of basic setup and and now we have the postgresql database set up so that's great and the next step is that we're going to set up the django admin the django admin provides a pretty convenient web api to let you edit content for your site and it and also manage permissions so this is basically for site managers and site administrators and it's kind of a handy way to kind of if you want to create objects and if you want to kind of edit content for your for your site or your project um it's an easy way to do it so let's go ahead and and set that up so the first thing is we're going to create a super user for the django admin um and so in that case we'll have basically a user for the admin that has all privileges so i'm going to run the command python 3 manage.py create super user um it's going to ask kind of you know what i want the username to be i just want to use johnsteen that's convenient email address that's fine put an email address put a password enter it again okay super user created successfully perfect um so then let's actually see what the admin looks like and um maybe create a few objects here so i'm going to go into python 3 there's py run server you should be familiar with that now and then to get to the admin we just go to the u the normal url and then we're gonna just backslash admin okay so um i think it has saved from other projects that i've done probably the same password so i'm just going to go ahead and maybe not go ahead and log in okay no thanks don't want to save that and actually i missed a step here because we i actually need to register um some of my models with the django admin so let's go back and do that to make sure we they actually show up in the admin okay so to do that we're going to go to admin dot py in our gifs app and then um we are going to make sure uh okay so admin is imported that's good and then we also want to import the models um which i will talk about that um but we actually haven't created the models just yet so why don't we come back to the admin once i've created the models for this um so we'll just scratch that and we'll go into the models a little bit and then come back to the admin all right so creating models models are basically the source of truth for the data in your database so models are basically python classes that serve as an api to your database and lets you structure and organize organize the data in your database generally there's a one-to-one relationship between models and tables in your database so if you want to create a gift table in your database you'd create a gift model and you're in the way that you structure that um here in python will will inform how the data is actually structured and stored in your database hey guys so i didn't want to rush through the models because i think there's a lot of info that's beneficial to go into there and i think that beyond about 30 minutes my attention span prevents me from really absorbing this much material once so if you want more information on the rest of the process i use to set up my app going into authentication how to create and use models how to create invitations to groups as well as kind of the rest of the process that i went through leave a comment below and just let me know that's something that you're interested in seeing it'll let me know whether or not this is a helpful tutorial or whether i should try something else that would be helpful for you so also if you want to keep in touch with future videos please subscribe below it'll just notify you so that way you don't miss the next video and anything else that you would like to see on this channel feel free to reach out to me email me directly my email is john s hey.com so i hope this was helpful this is a very quick way to get a django app up and running and honestly the overhead that this creates is one of my least favorite parts of making an app so the easier and in more intuitive i can make this process in the quicker that you can get started doing actually creative work on your app i think that is the goal for this tutorial so hope this was helpful i'll see you next time
Info
Channel: John Steen
Views: 6,494
Rating: undefined out of 5
Keywords:
Id: H1SriPJcjNA
Channel Id: undefined
Length: 27min 24sec (1644 seconds)
Published: Mon Dec 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.