Django: Getting Started Transcripts
Chapter: Your First Django App
Lecture: Connecting a URL to a view

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's open up Alexandria This is the default content of the Alexandria URLs file.
0:11 A comment at the top gives you some instructions on how to register a u r l. There are several different ways of doing this.
0:18 The mechanism I'm going to use is to import the function. I just defined in the primer.views file.
0:33 I then map a URL by calling the path function passing in the URL I'm registering and mapping that to the view that will handle it.
0:49 Path takes a string which is the URL. That would be the say hello. Notice the trailing slash, it's important. Don't forget it.
0:57 And a reference to the view that will be called when that URL is hit. In this case, also say hello. You can name your URL s and your views whatever
1:06 you like, but I tend to stick with this pattern. My URL s and views are usually named the same thing. It makes finding the code simpler,
1:14 when you've got a complicated project. I've got a view, I've mapped it to a URL. I'm pretty much set.
1:22 I'm gonna go start up the development server and visit the view. Here I have executed the run server management subcommand like before and
1:34 like before it's listening on local host port 8000 and inside the browser, typed in the URL and there you go. The world has been greeted.
1:49 Let's quickly review, you created the primer app to store your code by using the start app subcommand.
1:55 You registered the app by adding it to the installed apps list in the settings file. You then edited the primer file to create a view.
2:04 A view has to return some content. I showed you how to use the http response object to return some plain text. With the view in place, you map the URL.
2:15 The say hello URL to a view inside of us dot pi by editing the Alexandria URLs file. With all that in place, when you run the development server,
2:26 you can visit the URL that was registered and congratulations. you're a web programmer.

Talk Python's Mastodon Michael Kennedy's Mastodon