Django: Getting Started Transcripts
Chapter: Your First Django App
Lecture: Creating a view method
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I need to open up the views.py file now in the primer app. This is the default content, I'm going to replace all of it.
0:22
The purpose of the views file is to contain code that outputs a response that is mapped to a URL.
0:28
For this first example, I'll keep it really simple and not even use HTML. Just some plain text.
0:34
A view function in Django returns a response through an object called Httpresponse. This class is found in the Django http module.
0:45
So I've imported it on line 3. Now, let me define the view function. A view function must take at least one argument called the request.
1:02
This argument contains information about the http request. The name of the argument is only convention, but most people stick with it.
1:13
As I said at the beginning, I'm a traditionalist, so let's "Hello World!" The view function returns an Httpresponse object.
1:28
The first argument to the object is the content to be presented to the user. In this case, that's the message containing Hello World.
1:37
The mime type by default is HTML. As I'm going to be returning plain text.
1:43
I have passed in the optional content type argument setting it to indicate that the payload is just text. That's it. That's your first few.
1:53
Now, all you have to do is connect it to a URL.