#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: URL routing and your first view

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Let's see something loading in Django
0:05 instead of the launcher page.
0:07 And at first we are going to define the mysite
0:10 or the main app URL file.
0:18 And as you see, Django is really awesome
0:21 in their documentation.
0:22 It comes already with a lot of guidance and instructions
0:27 and some template code.
0:28 So this should be relatively easy.
0:34 And to meet the include.
0:36 And here you have the admin backend.
0:38 You can even rename this path because /admin
0:42 is pretty known, so you can make this
0:44 something lesser known.
0:49 And that's just to prevent people
0:52 trying to login on an admin backend.
0:54 So it's that easy to change a URL.
0:57 So now you would change this URL
1:02 to not be admin anymore, but mybackend.
1:07 So that's a quick fix.
1:11 What I really want to do for the quotes
1:13 is to redirect any request on the homepage, so blank
1:20 to go to the quotes urls.py file.
1:23 So I'm going to include quotes url.
1:33 So my main app is going to say any request to root
1:37 or to the homepage, redirected to the quotes urls.py
1:41 which we need to write next.
1:46 So inside the quotes app, there's not a URLs file defined.
1:51 I'm not sure why Django does not provide that file
1:54 but you surely want to have a urls file fore every app.
1:58 And later we will set up all the routes to the app
2:01 so the routes will create a new quote
2:04 the routes would get a list of all the quotes
2:06 the routes would edit the quote, et cetera.
2:07 But for now as we're looking at a hello world example
2:10 we're just going to see how a URL and view would interact.
2:29 And notice how it references views
2:31 that's the next component we're going to write.
2:39 A view would just have a couple of functions or classes
2:43 receives a request. And returns a response.
3:09 So this is the most basic view we can write.
3:11 We make a URL pattern to the index function
3:16 and we tell Django what to route to it.
3:18 So in this case just the main page, or /, or route.
3:22 So it's going to invoke the index function
3:26 which is coming from views.
3:28 In views.py we define an index function which
3:32 as per view convention, receives a view request
3:35 and returns a response.
3:37 And later we'll use the render Django shortcut
3:40 to render an actual template and embed variables in it.
3:44 But if you just need a simple response
3:46 you can just use the http response.
3:49 We're just going to send a string to the browser.
3:52 So let's see if this works.
4:04 So I'm going to go to the main URL
4:05 and there you go, welcome to Django.
4:08 So what happened is, first it went to the main app
4:13 urls.py, it was matching the root.
4:17 Then it included the quotes urls.py file.
4:24 It went looking there, and this is just the root URL
4:26 or /, but this could have easily
4:28 been welcome or something.
4:34 So in that case, the main URL would not have been occupied
4:39 or would give us a 404, and we would have had welcome.
4:45 So you see it is pretty easy to manage your clean URLs.
4:49 You can just define your slug, in this case welcome
4:55 and you set that in your URL patterns
4:57 as the first arguments to path.
5:00 You point it to a view function and at view function
5:03 you write here, again it receives a request
5:06 and returns a response.
5:07 So I hope this gave you a feel of the URL routing
5:11 and your first view, and next we are going to write
5:14 our first database model.