Locked lesson.
About this lesson
A web server allows us to run our own development project in real time. So in this lesson, we'll learn how to run our own web server.
Exercise files
Download this lesson’s related exercise files.
Run the Web Server.docx57.2 KB Run the Web Server - Solution.docx
55.5 KB
Quick reference
Run the Web Server
In this video we'll run the Django web server and view our new website in a web browser.
When to use
You'll use this command any time you want to view your Django project in the web browser.
Instructions
To run the Django web server, use the command:
python manage.py runserver
And then view the website in your web browser at the URL:
http://localhost:8000
Hints & tips
- python manage.py runserver
- http://localhost:8000
- 00:03 All right, in this video we want to run the web server.
- 00:04 So, Django comes with its own built in web server.
- 00:09 And what the web server does is allows us to actually run our project in real time.
- 00:13 So, as we're building out our website,
- 00:16 we can actually view it in our web browser, which is super useful.
- 00:20 Now, later on in the course, we'll actually push our code up to web hosting
- 00:24 so that the website is live online.
- 00:26 But until we actually build it out, we don't want it live online.
- 00:29 We just want it on our own local computer and
- 00:32 we want to be able to mess around with it, and work on it locally.
- 00:35 So, that's what the web browser does.
- 00:37 So, to use the Django web server, you just type in python manage.py.
- 00:42 And you'll get used to typing in python manage.py because that's
- 00:47 always what we type when we want to run a Django command, right?
- 00:52 Remember a couple of videos ago I said,
- 00:53 we always want to be in the directory that has our managed.py file.
- 00:57 This is why, so that we can run this file to actually run commands.
- 01:01 So, the command we want to use is runserver, and
- 01:04 we do this it says watching for file changes with StatReloader.
- 01:08 That means the web server has started, it's fired up, it's ready to go.
- 01:12 Now, to actually see our website in a web browser,
- 01:15 just head over to your web browser and type in localhost colon 8000.
- 01:20 And 8000 is the port number that the web server is running on.
- 01:24 So, when we do that,
- 01:26 we get a hopefully you'll see this little rocket kind of shaking around here.
- 01:31 This is the default web server page that comes whenever you create a new Django
- 01:36 project.
- 01:36 So, if you see this, it means your project was created successfully.
- 01:40 It means everything is working correctly.
- 01:42 And, you're good to go.
- 01:44 If you don't see this, it means you have an error.
- 01:46 You did something wrong along the way.
- 01:48 Go back and watch the last couple of videos and
- 01:50 make sure that you created your project successfully.
- 01:53 Now, if we go back to our terminal, you'll see stuff has changed inside the terminal.
- 02:00 And that's because as we call on the web server, things happen behind the scenes.
- 02:06 It actually fetches some stuff from our project,
- 02:10 favicon is a little icon that appears right up here.
- 02:13 So, anytime you do something, if we hit reload,
- 02:16 something will happen behind the scenes in this web server.
- 02:20 And you'll notice our command prompt has disappeared.
- 02:23 This is the command prompt right here.
- 02:25 It's disappeared right here, and we just see a blinking cursor.
- 02:31 Well, that's because this web server is continuing to run.
- 02:34 So, we can break out of here by hitting the control and
- 02:37 the C button at the same time.
- 02:39 And when we do, we see this migration thing.
- 02:42 We'll talk about that in the next video.
- 02:44 But you see now, the web server is no longer running.
- 02:46 So, now if we go back to our web browser and click reload,
- 02:51 we see this error, site can't be reached.
- 02:54 And that's because the web server has to be running in order for
- 02:58 us to be able to view our project, our Django project.
- 03:02 So, what I like to do is come back here and
- 03:05 hit up on my arrow key to do the last command and turn this server back on.
- 03:10 I like to just keep my web server running all the time while I'm
- 03:13 working on my Django project.
- 03:15 The problem is from time to time,
- 03:17 we're going to have to actually type in other Django commands.
- 03:20 And when we do, it's kind of a hassle to come here and hit Ctrl C,
- 03:24 break out of here and then when we're done, run it again.
- 03:27 It's just it's kind of a hassle.
- 03:29 So, what I like to do, I'll just open up another Git Bash terminal, and
- 03:34 I can pull it over here and you can see this one is still running.
- 03:37 The web server is still running in this one.
- 03:39 But now, we have this second one that we can run actual commands in.
- 03:41 So, we can go ahead and expand this second one,
- 03:47 and let's move back into our project.
- 03:51 So, that was mywebsite, and if we hit ls we see okay, there's the virtual.
- 03:55 And notice our virtual environment is not been turned on in this terminal.
- 04:00 So, we need to turn that on.
- 04:01 So, let's go source virtual scripts activate.
- 04:05 And when we do we see this little thing here.
- 04:07 So, now if we hit ls again, we're still in this directory, and
- 04:11 we need to be in this one.
- 04:13 So, let's cd into that directory, djangowebsite, okay?
- 04:18 Now, if we hit ls, we see this managed.py file.
- 04:20 And remember, you always want to be in the directory that has the managed.py file.
- 04:27 So, we know we're in the right place.
- 04:28 Our virtual environment is turned on, and
- 04:32 we can toggle back and forth to these two terminals if we want.
- 04:37 This one just has the web server running.
- 04:39 And it just continues to run.
- 04:41 So, we're just going to let it run, and
- 04:42 from now on renews the second one to actually type in our Django commands.
- 04:46 So, that's all for this video.
- 04:47 In the next video, we'll look at migrating the database.
- 04:50 I'll tell you what that's all about and why it's important.
- 04:53 And that'll be in the next video.
Lesson notes are only available for subscribers.