- Overview
- Transcript
2.2 Create an App and Run the Web Server
With your project created, the next step is to create the app that will contain all of your code. Once you have an app in your Django project, you need a way to actually run your app in a browser. So next you'll learn how to run the Django development web server to test your app as you code it.
1.Introduction2 lessons, 06:19
1.1Introduction00:53
1.2What You Need05:26
2.Django App Basics4 lessons, 24:54
2.1Install Django and Create a Project05:49
2.2Create an App and Run the Web Server05:15
2.3Responding to Requests06:29
2.4Creating Models07:21
3.Using a Database With Django3 lessons, 16:56
3.1Adding Models to the Database05:28
3.2Working With the Database06:29
3.3The Admin Portal04:59
4.Building Out the Lawn Care App9 lessons, 59:19
4.1Defining the App Routes07:08
4.2Rendering HTML07:27
4.3Rendering Data in an HTML Template05:08
4.4Displaying Available Fertilizers06:15
4.5Saving Time With the Render Function02:25
4.6Handling “Not Found” Exceptions06:26
4.7Using Forms and Saving to the Database09:05
4.8Adding Some Business Logic08:54
4.9Adding Some Style06:31
5.Conclusion1 lesson, 01:06
5.1Conclusion01:06
2.2 Create an App and Run the Web Server
The goal of this lesson is to simply create our first app within our project, to get a web server up and running locally so we can actually do something, and then actually display something on the screen. So it sounds like a lot. But it's actually quite simple. So what we wanna do first is we wanna create our first application. So like I said before, in the previous lesson, we created a project, which is really just a workspace where everything is going to live. Now we're going to create an app within that project. And an app is where all the actual work and the web application itself is going to live. So here I am at my terminal, and I'm within that lawncare project folder, that outside folder. And within there, I want to start by running a command. So I wanna create my app. So the way that I do that is I'm going to say python, and in my case, python3. I want to use that manage.py file that we talked about before. This is really like a utility that you can use to interact a lot with your project and with your apps. In this case, I want to startapp. So I want to start a new app within my project, and I can give it a name. In this case, like I said before, I want to create a web app where I can track the fertilizer that I'm applying to my lawn. So I'm gonna call it fertilizer, but once again you can call it anything you want. It's not a big deal. And I could create multiple apps within this project, like I mentioned before. So maybe this is my fertilizer app. Maybe I'll have a weed killer app, maybe I'll have a mowing app, things like that. So I can put a lot of different applications within this project and have them all hosted In the same place. So let's go ahead and hit Enter. And as you can see over here on the left-hand side within Visual Studio Code, I now have a fertilizer folder. And within here, I have a lot of different things going on. And we're gonna talk about all of this. We're gonna get into all of this in just a little while. So the first thing that I wanna do though is now that I have that app in there, I wanna get my web server up and running. And so why do we need a web server? Well, we're building a web application. So I wanna be able to see the work that I'm doing. And one of the nice features of Python and Django working together is that I can have a web server running in the background that's constantly updating and showing me the changes that I'm making to my website in near real time. By near real time I mean if I'm changing existing files, then this web server is gonna notice that. And it's gonna reload my application and show the changes on the page. But for the most part, that will work pretty well. In some cases, if you're adding new files or doing other large scale changes, it might not always work. So you might have to refresh the page or you might have to restart the server. Things like that happen, but for the most part it's a pretty good way to do development here. So once again, within that outside lawncare folder, I'm gonna use Python, in my case python3. I'm gonna use manage.py again, and I'm gonna say runserver. Now this is going to go ahead and start up a web server. And you're also gonna see that in the background over here, it created a file called db.sqlite3. Now that's gonna be our database. That's our SQLite database. That's where all of our data is going to live. We're gonna dig into that a little bit later, but you're gonna see something here that we're going to kind of, we're going to talk about quite a bit later on. But you should probably see some red text here that says something to the effect of having some number of unapplied migrations. Now, migrations are changes to your database. So one of the nice things also, and actually one of my favorite things about Django, is that it's going to have a lot of kind of things done for you. Considering or around things like administration pages, being able to add and create users, being able to log in, being able to do a lot of that type of boilerplate stuff that is honestly a pain in the butt. When it comes to building web applications, cuz a lot of times those are things you have to do on your own. So Django kind of handles a lot of that for you, which is pretty cool. But we're gonna come back to the concept of the database, SQLite and migrations in a couple of lessons. But just to know that that's where that's coming from. There's nothing wrong, that's absolutely fine. But we're gonna come back to that a little bit later. But the important thing to see here is this line right here that says we started a development server on http://127.0.0.1, which is the loopback adapter, which can also be referred to as local host port 8000. So let's go ahead and open up a browser. So in my case I'm going to open up Chrome. And I'm gonna go ahead and paste that URL into my browser. And you're going to see that you have successfully installed and started running Django. So now, as you can see here, we have Django up and running. We've got our web application running. So we can actually see something. This is some out-of-the-box boilerplate stuff that they can kinda give to you so you can see something up and running. You can obviously go ahead and check out the Django documentation, follow along with a tutorial, which I would highly recommend if you haven't already done it. And get some more information from the Django community. But now that we've come this far, now it's time to actually start to get rid of a lot of this boilerplate stuff, the stuff that comes with the installation, and start to build our own custom application. And we're gonna start doing that in the next lesson.







