Introduction & Server Recap
Welcome back, aliens. My name is Naveen Reddy, and let's continue with this series on Django. In the last video, we have seen how do you set up Django in your machine—on Windows, at least. And then now, let's write a code. Because in the last video, we have just created the project and then we were running a server, a lightweight server which Django provides. It was running.
So you can see this is the code which we have done. We entered this command, which is python manage.py runserver, which will run this server. And then you just have to use any browser which you like—I am using Chrome here—and you just have to enter the IP address. Now, this is the IP address which is basically localhost, and the port number is 8000. And we are able to run the server and then we get the output as well.
But then we don't want this; we want something extra, right? We want to build a web application. That's what we do, right? So when you say you are using Django, it means you want to build a web application.
Static vs. Dynamic Pages & Project Goal
Okay. Now there are two types of web application we can talk about: we have static pages and then we have dynamic pages. Now, all the websites which we use nowadays, some of the pages which you visit are static pages, which means everyone will see the same page. Dynamic pages are different for every one. Example, if I go to Facebook—in fact, we have talked about this example—so if I go to Facebook, I will see my own feeds, I will see my friends' feed. You will not see my friends' feed, right? You will get your own page there. So if I go to my bank account, I will see my own bank balance, not your bank balance. So basically, we have a dynamic content there.
Now when you say you want to build static content, we can use HTML, CSS, JavaScript, done. But when you say you want to make it dynamic, you want to do some processing, that's what we are doing here. So let's make a simple web application. Okay, when I say simple, we are going for the basic. You know what we do when you learn how to create a web page? You write 'Hello, world!', right? That's what we do.
Now I know you are expecting something big, but the thing is, here we want to learn Django. We don't want to learn the complex programming at this point. We know Python, so we can write big applications, but now we have to understand the framework. So let's build something using which you can print 'Hello, world!' on the page. Yes, you can do that with simpler methods as well, but let's make it dynamic here.
Setting Up Visual Studio Code
Okay, so for that, what I will do is you have to use some sort of IDE. Now, basically when you say IDE, we can also use our notepad to type the code. Okay, that's completely fine. You can use Sublime because that's what a lot of people use. Here we are going to use Visual Studio. You can download Visual Studio by going to Google. So when you search for Visual Studio download, this is what you will get. Just go to their website and you can download. In fact, every new version, they are providing amazing features. So let's try it out. One of the best IDEs available for programmers now.
But again, the Community version, which is free. If you want to go for a Professional version, you have to pay for it. But at least at this point, we are happy to work with the free version. It works well. So just download that and install. Simple steps. Now in this machine, I already have Visual Studio, so let me just open that.
So you can see we have Visual Studio here. This is how it looks like, the dark theme. You can also go for the light theme, it's also available here. It depends... some programmers normally prefer black or dark theme. YouTubers or the content creators, they prefer the white theme because the recording works well.
Okay, so now once you have Visual Studio, we have to open the project. Now if you remember, in the last video, we have done that. We have created the project that is Telusko. So let's open that folder. So I just have to go to the folder 'projects'. Remember, we have created this folder 'projects', and inside this project, we have a Telusko project which we have created using the command. And of course, select that, Telusko, and click on open. This is what you will get.
Exploring the Django Project Structure
So you will get your project here, which is Telusko, and inside this, you can see we have a lot of files. In fact, we have talked about them before, but just to go for a quick recap: We have this manage.py, which is useful to manage your project. And then if you want to do some settings in your project, this is very important, the settings.py file.
So if you open this file you can see we have a secret key, which is basically used for deployment in the server. And then we have debug. Debug will give you a lot of information when you get stuck somewhere, and this is very important, actually. So let's say if you are building our project and if you want to know what is happening behind the scene, just make DEBUG = True and it will give you all the information. But make sure before deploying on the server, just make it False, because someone can hack your machine and they can see all the logs.
Okay, and then this is important, this is the installed apps. So in your project, these are the apps already installed. Again, we'll talk about it later. We can have our own apps. So these are the middleware which will help you for security purposes as well. We have a template, so you can see this is the Django template, will help you to provide template stuff.
What about databases? Of course, right? If you are making a web application you also want databases behind the scene, maybe MySQL or PostgreSQL or SQLite. So by default, it gives you SQLite, but you can also use PostgreSQL there. And then we have some password stuff here for validation.
So let's not focus on this file as of now because we don't actually edit this file very frequently, so let's keep it there. But it is a very important file. Okay, and then we have URLs. So you can see we only... we might be having only one URL here, which is for admin, for admin stuff. So that's it as of now. Let's create our first app.
Understanding Django Apps vs. Projects
But hold on, you might be thinking we got a project, right? Why do we have to create a separate app? The thing is, in Django, what we do is we have a project. Like, let's say if I want to make a big web application which will have a lot of features—you know, the admin stuff, or for a let's say, e-commerce website. We can have 'product' as a module, we can have 'shipping' as a module. So we can have different... those are different apps.
And all these apps in the website will be part of one particular project, which is your website in general. So login stuff will be one app, then showing the products will be one app. That's how it works.
So let's create a simple app here. So the way you do that is by going to our terminal. So you can see this is the terminal and this is where you can type.
Creating Your First Django App
So, how do we create an app and what you will name it? So as of now, I just want to name it as 'calc' because after 'Hello, world!', the next example will be 'calc'. So let's create a calc app. To do that, we have to type a command. So I have to say python manage.py... That's the file we are using and then you have to mention the name of your app. So as of now, we'll go for 'calc'. The moment you say enter... we got an error.
The problem is now we are using a different command prompt. I was not expecting this, but then we are using a different command prompt, right? Initially, we were using the Windows command prompt, now we are using it through Visual Studio. And then if you remember, we have to also set the virtual environment. So the virtual environment we were using here is 'test'. So we have to use that. So I will close this and we'll use 'test'.
So how do you use our environment? You have to say on test. So now we are in 'test'. Now we have to fire the command. So we have to say python manage.py... Oh, we have forgotten one more thing. I have to say startapp. I have to say startapp and I have to mention 'calc'. The moment you say enter, now you can see on the left-hand side, we got a 'calc'. This is basically your app.
Inside a Django App's Structure & Next Steps
So this is your project, Telusko, and this is your app. Now if you can see, we have some different files there. We have a settings file here, but we don't have settings here because this settings is for the project, and every app will have its own stuff. Example, let's say if we talk about the app, we have some admin stuff here. We'll not be doing that as of now, but we have admin stuff. We have apps.py. This name is important. Just look at this name. We are getting a class named as CalcConfig. We are going to use it.
Then we have models. Now if you talk about Django, if you want to build a web application, of course, you want to work with the database, right? And in Django, we have this awesome concept named as ORM. So we will talk about that later, but then this model will be used for that, or basically, it is used to hold data.
And then we have tests. So if you want to write some test cases, if you want to test your application, this is how we can do it. And then we have views. Remember when we send a request, we have talked about MVT where views will handle your request, so you can do that in here. Okay, but we want to build 'Hello, world!', right? So how would you do that?
That we'll see in the next video. So in this video, we have talked about how to create an app in the project in Visual Studio—of course, that's what we are using here—and then we have seen the files. In the next video, we'll run this code. So in the home page... so for example, if I go to the home page and if I refresh, I should be getting something like 'Hello, world!' or something like that. This is not working now, but we'll make it work. We want 'Hello, world!' here at the home page.
So that's it from this video. I hope you enjoyed. Let me know in the comment section and do subscribe for further videos. Bye.