Launch Your Local Server

Out of the box, django already has a lot of powerful functionality that we are going to be utilizing in Part 2. In fact, it already has a dummy web page setup as soon as your start a project. However, you might be wondering how to actually turn the files and folders in instaclone/ into an actual website that we can go to.

In order for a website to be accessible in a browser, there has to be a server running. Let's go back to our picture from the "How the Internet Works" section:

In this picture, the server is running in Oregon on the google CPU. Eventually, when we launch or website onto the internet for everyone to access, we will have to put our code on a server in Oregon as well. However, during development we don't need to jump through such hoops. Instead, we can launch a local server which will allow only our own computer to access the website.

Manage.py

We will be running commands through the terminal to launch the local server. Most commands we run to make our django app do things will be of the form:

python manage.py <command>

Thus, you should make sure that your terminal is in the directory instaclone/ which is inside of your virtualenv (the directory with manage.py in it).

Launching the Server

First, before we launch our server, we have to run one setup command (don't worry about what this does, we'll be covering it later in Part 2):

python manage.py migrate

The command to launch the local test server is:

python manage.py runserver

Once you run the command, it should look like the image above. Notice how your can't run any more commands. This is because the terminal is in an infinite while loop of sorts. This, in essence, is the server: an infinite while loop waiting for request to be made to it.

Accessing Your Website

In order to access the website, you have to go to our computers local URL. This is:

127.0.0.1:8000

This URL is two parts: (a) the local IP of our own computer and (b) the port at which our web app is running. In effect, the picture from above changes only slightly: now, instead of interpreting a URL and going over the network to find the server, our computer realizes we are accessing our own local server and so it just gets the information from the server running on the computer itself.

When you go to the website, you should see this:

Additionally, i you go to your server running on the terminal, you'll see the request we just made to it to load this page:

Throughout development, you can keep your local server running the entire time. Django will automatically re-launch the server whenever we make a change to the website code so that it is reflected in the browser. I advise that you have two terminals open; one for running commands and one for running the server.

Conclusion

By running the command "python manage.py runserver", we were able to launch a local server and access our web app by going to 127.0.0.1:8000.

Congratulations! You have officially made your first Django web app! Unfortunately, it is not yet a very interesting web app. In the rest of Part 1, we will go through the steps of making your own web page in this web app.

results matching ""

    No results matching ""