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

Talk Python's Mastodon Michael Kennedy's Mastodon