Deploy Django App on Azure #1: Changing Django Code for Deployment

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to a new tutorial series on the CVI Channel in this series we'll be focusing on the deployment of a Django application on Microsoft Azure you can see the app that we are going to deploy on your screen now is a very basic application with the home page and also an about page and on our home page you will see a number of records that have been added to the database we also have the option to add records and to delete records and that is what we will be deploying to Microsoft azure so how will this deployment practically work well we start off in our local computer where our project is stored in a folder we are going to bring that to a GitHub repository using git and this is kind of the online storage point for our code and when we make changes on our local computer we'll be able to push those changes again to our GitHub repository so if we make changes locally we can bring those online very easily um then from GitHub onwards we're able to connect our code to Azure um on Azure we will set up a few resources and I will explain those in a later video and there we can simply say that it needs to use the code in our GitHub repository this tutorial Series has been split up into four different parts you are now watching the first one where we're going to make some changes to our Django codes for deployment um in the next video we will push our code to GitHub using git video after that we'll be focusing on creating all of the Microsoft Azure resources and in the last video we will deploy our code to Azure and see where everything works the way that we expect but in this video we are making changes to our Django app for the deployment and to realize this we're going to do five things we start by installing additional packages for deployment next we will be creating a settings file specifically for deployment because some things are going to change such as our database for example the third item is telling our app when to use our deployment settings and when to just use our local settings so we can use the app in both our local computer but also on the Azure portal um we will also add a requirements.dxt file which will present a list of all of our packages so as you will know which packages we have used during the development and as the last item on the list we are going to add a config and git ignore file the config file will set some variables on the Azure portal and they get ignore file will say which parts of our code need to be going to GitHub and which parts do not so let's get started the first thing that we need to do is installing some additional packages so I've already opened the web page where we can find them the first one is psychop G2 and this will enable us to communicate through the postgresql database that we will be using in our deployment so this is the first package we need to add to our app so I'm just going to copy it go to my code and say clip install cycle G2 and that is going to take some time to do that and meanwhile we can go to the second package we need in the second package is python.env and this enables us to use environment variables actually read those um So within our Azure environment we will set a few a variable such as a secret key such as some database credentials and we need this package to be able to get those from Azure and actually use those in our code so again I'm going to copy it to the clipboard feel free to just type it in yourself and spit install python hyphen.env and the last package that we will need is white noise and white noise takes care of your static files so it enables us to use our CSS files but also images and all the different kind of things for our app when it is in deployments so we are going to copy it install white noise and we are going to paste it inside of our code there we go and now all of the packages that we need have been installed the next item on our to-do list is adding a new settings file for our deployment settings so right now we have the settings the by file which you also will have inside of your project but it has all of the settings that I'm going to use for my development on my local computer but some things are going to change when I move to Azure so for example we will use a new database we do not want debug to be settled through because then everyone can see all of our settings when something goes wrong there are some other things that we just want to change because it's different hosting your app on your local computer than it hosting it online um however we do not want to override all of these settings we just want to identify the settings that will be new um or different in the online version then it will be on the local version and that is exactly what we are going to do by adding a new file in your same directory as your settings and file and we're going to call it deployment that's high and in this file we are going to list down everything that is going to be different opposed to settings on our local computer so let's add some code to our deployment at Pi file we're going to start off with a few Imports which we will need so first things first we're going to import OS which will allow us to communicate with environment variables next from that settings we are going to import everything the star means basically everything from the our local dot settings file that we use now and separately we're also going to import base there from our settings file and for some reason uh yeah that we need to do this separately base there refers to our base directory to evolve to our to our base directories um so let's list down the variables which are going to be different for deployment than when we are going to use this code on our local computer and the first one that we need to fill in here is the allowed hosts now the allowed host tells us a little bit more about which URLs or domains uh can be served by this code so it actually tells which URLs domains can use this code and we're going to set that to an environment variable so we're going to type in OS Dot and fire and then we're going to set this to website hostname and the reason we're setting it to website host name is when we deploy this application to Azure Azure will automatically create this hidden environment variable which specifies our URL and by including that here we basically specify that only the URL that Azure has created can be used to serve these codes no other domain can use this code the next thing that we are going to set is the csrf trusted Origins and this is going to be very similar we're going to set that to https and then again we're going to add OS dot environment and the website hosting so this will just give us the full address and the series of exclusive origin specifies uh The Trusted URLs or domains from which requests can come into our application next we're going to set debug and debug we're going to set up to false because always when you deploy an application you want to set that to false now when debug is true when your user makes an error or a mistake in the application it will display what is going wrong it will also show you a whole lot of uh things of your code which you might not want to be out there on the internet so it could be database credentials or some other important information that that's just for the developers not for the people using the tool if you set this to false it will show a very generic 404 page and it will not provide any information uh yeah on the actual code and we can illustrate this by going to our application if I were to go to a path from which I know that it will not exist I will get this page not found 404 which it specifies all of the different files I have inside of my application and on the bottom you can already see that we're seeing this because we have debug it's true in our local settings if we change that to false it will display a standard 404 page and no additional information will be displayed there so that is much safer the next thing in our code which is going to be different than our local settings is the middleware um and to input this inside of our settings file we're just going to copy it over and make a small change so from your settings file copy over the the middleware uh brackets and when we go to deployment we're just going to paste it and make one small change and that is going to be the addition of white noise because white noise is going to handle some of our static files so on the second line we're going to specify right noise Dot middleware that white noise middleware now it's quite important that you take into account capitals here because this is the way that your opponent can go wrong again so let's check it one more time White Noise dot middleware that white noise middleware with the capital letters there another thing that we need to state is our static files storage this is going to be very similar again it's also going to be from White Noise because White Noise will take care of that real and specify white noise that storage that compressed with the capital manifest with the capital and then aesthetic with a capital and then files together and Storage all right and we also need to state where we are going to store those files and it's going to be in the static route and then we're going to set that to os.pot dot join and we will state that we want to follow our base directory and then sort it all in a folder called Static files so now this is a folder it's going to be created and that's where we can put our static files the last thing we need to specify are our database credentials this is really important because we're going to use postgresql instead of the database we use now which is sqlite so we start by say declaring databases it's going to be equal and we open some parenthesis then we say default and similarly to we just did we again open Berlin City we need to specify a number of variables first of all we need to specify the engine that we're going to use and you can see in our settings apply file on your databases that before it was django.db the sqlite free but right now we're going to switch that we're going to use django.vb dot backends dot post rescue well there we go and for this database we need to specify a number of credentials so we can actually make the connections when we send requests so it requires a name of my database it requires a host for database it's going to put these two empty values for now we are going to need a user close to one and lastly we will also need to provide a password foreign is going to provide us with all of these credentials when we create these database and uh as a matter of fact they go even further they are going to create an environment variable that will store all of these credentials so we can refer to that environment variable instead of hard coding the name the host the user the password since of course it's not safe however the way that they provide these credentials is a little bit different than what you're used to they provide an environment variable called Azure postgresql connection stream and this connection string is one complete string which specifies the server the name the also the port also the the password and the user ID now since it is all into one string we need to split this string to use these variables inside of our code and that's exactly what we're going to do now so the connection string is equal to OS dot environment because we're going to get an environment variable and as we just saw it's going to be azure close press ql and then it's going to be connection string now we need to actually uh get those parameters false splitting these strings into some bars so let's create a code to split it and we're going to do uh split equal to an equal sign um next we're going to say uh that's okay it's going to be actually the same code so let's just copy the one before so we're going to do again power dot split based on an equal sign within the first one so uh in connection string but split like this this should be your end result so our connection stream variable gets the Azure postgresql connection stream with all of the connections we need and this parameters variable uh splits that string up into different chunks so we can actually use it inside of our name host user and password and that is what we're going to do right now so inside of the name we're just going to specify parameters and then equal to PB name and we're going to do the same for a host we're going to call it host same for the user called user and the same for password and this is our database done and these are actually all of the things that we need to specify inside of our deployment python the next step is to tell our Django application when to use our settings the by file and when to use our deployment data python um and to do that we're going to change two files first of all the wsgi.bifer and second of all the manage.bi file and why do we are we going to change these two ones well if you take a look inside of this uh python file you can see that it sets the default settings to Azure projected settings and this will also take it to our cloud service meaning that it will take Azure project settings and we are going to slightly modify this code so in this file I'm going to say the settings module then go into state that I want to use my azure project dot deployment file if the website hostname yes correct if the website does hostname is present inside OS plus environment else I would like to use my Azure project dot settings file so what we do here is very basic we refer to the Azure project folder which is my folder here and we State we want to use the deployment file if the website the hostname is there and I already explained that earlier that the website of hostname is a variable that I hear sets by default so by evaluating whether this is here we can determine whether we are in deployment or whether we are from our local computer um and then it is actually very easy because we can say that this uh piece of code here should be equal to not Azure project dot settings but to our settings module and this will create a clear distinction between when to use which file next step is to create a requirements.txt file and this file is going to tell Azure during the deployment which packages we have used to build our application next step is the requirements that the XT file we are going to create that and it will consist of a list of all of the packages that we've used during development Azure needs to know this because Azure needs to install all of these packages to make sure that the code will work as well as it does on your local computer as it will do the board online so how do you generate this well it's really simple uh you actually just need to state yep freeze then click this and we're going to freeze it into requirements.txt and you can see immediately a file pops up right here and you will see that it has all of the packages including the version that we've used now you can also see here the packages that we have added at the beginning of this tutorial we're also going to add a config file um so in the lowest level of our application I'm going to add a new file and I'm going to call that dot production because that's the name that Azure will recognize and on top of this file I'm going to specify that this is a config file and inside of here we're going to specify one variable that will be set into the environment variables of azure when we deploy this and that variable is SCM do build during deployments we'll just set that to true and then save it just make sure that when we deploy this it immediately builds the application this is critical because otherwise it will not work but this is all you need to do Azure will recognize that this is there and start building your app when will you deployment the last video we're going to add another last episode tutorial is a git ignore file so we're gonna again on the lowest level of our application click on new file and create a file called Dot get ignore and what this file will do is when we are going to use git to publish this code onto GitHub it is going to ignore the files that I'm going to list down here now this list is quite big and I've actually found this another GitHub repository of Microsoft so I'm just going to paste all of the values here um I would recommend that you go to my GitHub repository and look for the git ignore file there and just copy and paste it so you make sure that everything is there so one of the typical things that you don't want to take into your GitHub is like the buy cache but also for example your database files all of these kind of things that they don't need to be added to your GitHub repository so therefore we exclude them by using this get ignore file so that was it for this uh first video inside of this tutorial series we've successfully changed our Django code for deployment and that means that in the next videos we can start and doing the rest of the steps we need to do in the next video we will push our code to GitHub with Git so that our code will link online and videos after that we will create the Microsoft Azure resources and deploy our general code to Microsoft Azure thank you very much for watching this video and I hope to see you in the next one
Info
Channel: CBI Analytics
Views: 5,932
Rating: undefined out of 5
Keywords: Django, Azure, Python, Deployment
Id: QjEVmQ4rcWA
Channel Id: undefined
Length: 21min 58sec (1318 seconds)
Published: Tue Aug 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.