Views (views.py)

Now that we have matched the homepage URL, we have to make the URL Handler. In django, the piece of code that handles a specific URL is called a view. In this section we will make a view for the homepage and, in the process, see what it means for a URL to be handled.

GET and POST Requests

Before we go into the specifics of how to make the view, we first need to understand the different type of requests that a user can make to our web application. For the purposes of this course, there are two different requests that a user can make:

1) GET Request: this is the only type of request we've talked about so far. It is when the user goes to a URL in our app and asks for the server to get them some static files like HTML, CSS, and JS. This type of request is characterized by the client asking for data/files from the server.

2) POST Request: this type of request occurs when the client sends data to the server to be processed and, possibly, stored. For example, when you fill out a form to login to a website, you are making a POST request with your userID and password asking the server to process the login data to authenticate you. When you make a new status update on Facebook, you are making a POST with the text of your status asking the server to store the text as a part of your profile.

We will get into the details of how to handle post requests in the Part 2. For now, we will only concern ourselves with get requests.

Make views.py

All of our views are going to go into a views.py file inside of the inner instaclone/ folder (The same folder where the urls.py is located. Simply make an empty python file and name it views.py:

Importing the View Class

In the latest version of django, a specific view is actually a class we create that inherits from Django's View class. To do this, we will have to import Django's view class. Put the following import lines at the top of your file in order to do that:

from django.shortcuts import render
from django.views.generic.base import View

The first import will be used later, but the second import is the View class from django that we are going to inherit from and specialize for our purposes.

Creating the View Class

Define the HomePage view in your views.py as follows (we'll go through and understand each part):

class HomePage(View):
    def get(self, request):
        return render(request, 'index.html')

In the first line, we are simply defining our custom view to inherit from django's View class.

Within the HomePage view, we need to tell django how we want to handle GET requests. In order to do that we override the "get" method. The get(self, request) method, in addition to taking in self, also takes in a request object. We don't have to worry about the request, but just know that it stores specific information about the client that made that GET request. For instance, it will tell us the IP address of the client, it will store any data they may have sent us (useful for POST requests), and it will give us any user login information if the client is authenticated.

Finally, we define the get(self, request) method does one thing so far: it tells django which HTML file to send to the client. We do this by returning the render function, which takes in two things: (1) the request and (2) the name of the HTML file to render for this page. We have not yet defined index.html, but we will do that in the next section.

Linking the URL to the HomePage View

Now that we have made our HomePage View which will handle the / URL, we have to tell django to use this view when the user goes to /. To do this, we have to go to the urls.py and import our HomePage view. Your urls.py should now look like:

from django.conf.urls import url
from django.contrib import admin

from instaclone.views import HomePage

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', HomePage.as_view())
]

In Django, when you import something from another file in your application, you have to always assume you are starting from the highest level folder in the app, the upper instaclone/ folder. When we say instaclone.views, we are essentially says the views file inside of the instaclone folder.

As you can see, we put the HomePage class next to our / handler. We have to say HomePage.as_view() for reasons unknown to be but django says we have to and so we will.

Conclusion

In this section we learned about GET and POST requests, created the views.py folder, created our HomePage view to return the index.html, and then linked our / URL to use the HomePage view when the user goes to /.

Thus far, your urls.py should look like:

from django.conf.urls import url
from django.contrib import admin

from instaclone.views import HomePage

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', HomePage.as_view())
]

And your views.py should look like:

from django.shortcuts import render
from django.views.generic.base import View

class HomePage(View):
    def get(self, request):
        return render(request, 'index.html')

Now that we have recognized and handled the / URL, the final step is to make the HTML file, index.html, so that Django can serve it up to the client when they come to our home page!

results matching ""

    No results matching ""