Previous Lesson: Lesson 5: Start a New Project in Django
In this lesson, we’ll be running our Django server and make our browser look like what you can see in the featured image.
In the previous lesson, we’ve created a new project: wordcount. But, when we had opened that folder, we saw these:
“What are these? And where did these file and folder come from?” Did you remember that we had implemented a django-admin startproject on our Terminal? That gave us the bare-bones we need to start a project.
Open up your Terminal. Remember the shortcuts Control + `? Hit it:
Good. Currently, we are at our Other Projects directory. Now let’s see its contents using the ls (or dir in Windows) command:
Nice. Now, let’s get inside that wordcount by doing this:
Great! We’re now inside our project! Let’s check and see the contents of this directory:
Here, we see the manage.py and wordcount. You might notice that I have a db.sqlite3 and you don’t have it. That’s because I have run this server myself before. So never mind that; you’re going to get it too once you’ve run your server.
So, what is the purpose of the db.sqlite3? It is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. In short, it is a database.
And what’s the purpose of the manage.py? It is our tool for executing many Django-specific tasks. For example, starting a new app within a project, running the development server, running your tests, etc.
Running our Server in Django
Let’s now start running our server. We’ll type python manage.py runserver. Type it exactly:
And then press ENTER:
This is gonna start up a little web server on our computer that is hosting our Django project. Here you’ll see the http://127.0.0.1:8000/. If you’re using Visual Studio, or if it is possible in your Terminal, you could hold on Ctrl and then click this link.
Or, you can just copy this link by hitting the shortcut keys Ctrl + C. Then open up a browser, paste it to the URL, and ENTER:
Boom! If you see this screen on your computer, this means that your website is up and running on your local computer!
You may be wondering: “How in the world was this possible?” Back on our Terminal, we saw the http://127.0.0.1:8000/. Every time you type in the 127.0.0.1 on your web browser, that will refer back to your own computer. If you’ve already done any other web development, you will be familiar with these numbers.
“I’ve never seen a website that has a colon,” you wonder. This colon just gives the site a port number. In any website, they have their own port number and by default it’s ‘:8000.’
For example, if you’ll type in google.com:8000 on any web browser and hit ENTER:
But we don’t have to include that :8000 because it’s set to default on any website.
Note: If this didn’t work on your computer, you can check out this reference.
Ctrl + C to Stop the Server from Running
To stop our server from running, press Control + C:
Now, go back to the website on your web browser, then refresh it by pressing f5 or Command + R on Mac. Let’s see if we still have that page working:
Okay, we can’t reach a site without our server running. So we need to restart our server.
Restart Our Server
If we want to start our server back up again, instead of typing the whole python manage.py runserver again and again, we could simply press the UP ARROW. Try it:
And then press ENTER:
When we reload our page using f5 or Command + R:
It’s working again!
Misconception in Programming
When you learn how to code/program, you’ll find so many specific codes and commands that you’ll need to exactly type in your editor to get your code working. But beyond that, there’s a big misconception that people say about programming.
Misconception: “As a programmer, you need to memorize everything because coding/programming is just a hundred percent typing, typing, typing until you could make something, and then boom! That’s it. You’ve just created your own big project.”
I’m gonna tell you that that’s not true. Yes, you have to make your code exactly right and true, but you don’t have to memorize everything. That’s why Google there for us. We can always search for the things that we need to know in coding. The courses Learn Python and Learn Django are some of the great tools that we can use.
So never have a pressure like thinking: “Oh how am I going to remember how to write like, you know, the ‘django-admin’ blah, blah, blah?” NO! Whenever you got a question, whenever you forgot how to do something in programming, Google it, come back to this course.
Conclusion: Whatever it is you gotta do, just remember that there is no requirement at all to have all these stuff in coding memorized. It is counterproductive for everyone to memorize everything because what’s the point of having everything memorized but you’re not learning new things?
Programming is all about creating new things with having just a few things in your mind to get things runnin’. Remember that.
Did you find running our Django server easy? Good. If you have any questions, post them on the Comments section below, and I’ll try my best to answer it. On the next lesson, we’re gonna have a project tour; we’re gonna talk about every single thing that can be found inside that wordcount folder. See you there!
2 Replies to “Lesson 6: Running Our Django Server”