#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: The quotes app url mapping

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Welcome back to Django One, day three.
0:04 The coming days, we're going to further flesh out
0:06 the quotes app
0:07 and we're going to look at forms
0:09 the admin backend
0:12 function-based views.
0:13 So we're going to go from the URL mapping
0:17 to the view to the templates
0:19 and of course that involves querying the Django ORM
0:23 as we learned at the end of day one.
0:26 And we're going to refactor it in class-based views
0:28 which is a bit more advanced
0:30 but I just want to make sure I cover it
0:32 because in Django you really can opt for
0:35 the function-based or the class-based views
0:38 and you might like one or the other.
0:40 It's just good to know about them.
0:42 Actually it's here already on the slide
0:43 class-based view is pretty concise.
0:47 And finally when we talk about templates
0:48 of course you want to make it look nice
0:50 so we're going to integrate the mui.css framework.
0:54 And that will be covered in templates and static files.
0:57 So there's a lot to cover, let's dive straight in.
1:01 The idea is to first cover the URLs
1:03 needed for the quotes app in this video.
1:06 Next we're going to look at forms
1:08 then we're going to look at views to get our templates
1:11 and finally we add the quotes app
1:13 to the admin backend.
1:14 Which is convenient to be able to
1:15 handle quotes, objects from the admin interface.
1:19 Back at my terminal to recap
1:21 we have a quotes project, the file-based SQLite
1:24 database is sitting at the top
1:26 we have our manage.py to run the server
1:28 and handle migrations as we saw
1:30 at the end of day one.
1:32 We have the main app or central project app
1:35 under my site which contains the configuration
1:37 in settings.py
1:40 We have our quotes app which we're going
1:42 to work with in the coming videos
1:44 so I'm going to cd into that
1:46 and here we see all the components
1:48 that make up a Django app.
1:50 So let's start with urls.py
1:54 here we still have the hello world code.
1:55 So I'm going to wipe that out.
1:57 And here we want to define all the routes
1:59 for our quotes app. Quotes is a typical CRUD app.
2:02 So we have five different things
2:04 we want it to do: List all the quotes
2:07 list an individual quote
2:10 add a new quote
2:12 edit an existing quote
2:14 or delete an existing quote.
2:16 So we have to set up five routes
2:18 to the corresponding actions.
2:20 Which will be functions in the view.
2:23 The way to do that is to use path
2:26 and the first argument is what
2:27 the URL pattern will be.
2:29 So if you have main app.com/something
2:33 that something is what we define here.
2:36 Leaving it empty means that the page URL
2:39 will just hit that view.
2:42 And to clarify that the page URL when
2:45 the server is running is just this
2:48 and a URL pattern could be
2:51 for example we talk about quotes
2:52 We can have quote number eleven
2:55 In the functions we will write in the views
2:57 we reference them like views function.
3:01 I should see the views we import here
3:04 so we're going to write a quote_list function
3:08 in the views in an upcoming video.
3:11 And we also pass it to the name argument.
3:21 So what this means is when we hit the main URL
3:23 again that will be this
3:27 the URL mapper is going to call
3:29 quote_list a function that's defined in
3:34 views.py
3:37 and similar to that for the other
3:39 four actions I mentioned we're going to have
3:41 a similar pattern. So for an individual quote
3:48 for example quote eleven, we need to specify
3:52 that like this in less than greater than signs
3:56 type integer and here we can call it anything
3:59 like variable name
4:02 which typically is a PK or primary key.
4:06 And we can pass that to
4:07 the view function so this will be called quote_detail
4:13 and here I can just repeat quote_detail.
4:16 So when we hit
4:19 base URL slash eleven this urls.py mapper
4:24 will match this pattern
4:27 and route it to the quote_detail function
4:32 in the views. And the other three are similar
4:37 if we match slash new
4:41 it's going to call quote_new
4:49 if we match edit/int primary key
4:55 it's going to match quote_edit
5:03 and the same for delete.
5:04 If you match a URL called delete integer
5:13 you're going to match quote_delete.
5:19 You're going to match quote_delete.
5:23 Finally I give it a namespace of quotes.
5:30 Just as a way to isolate
5:31 if I have many apps to make sure that
5:34 none of these routes or functions clash.
5:37 Now all these patterns are tied to a namespace.
5:42 Which you might remember
5:46 namespaces are one honking great idea.
5:47 Let's do more of those.
5:51 Okay, so we have our five routes defined.
5:53 And the next step is to start writing those
5:55 funtions so quote_list, quote_detail
5:57 quote_new, quote_edit, and quote_delete.
6:00 But first I take a quick detour to show
6:03 you how to define forms because when we
6:05 add new quotes and edit existing ones
6:08 we want to work with Django forms to save
6:12 a lot of code and make it very convenient.
6:14 So next up are forms.