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

Talk Python's Mastodon Michael Kennedy's Mastodon