URL Converters - Flask Web Development with Python 28
Key Takeaways
This video tutorial series covers Flask web development with Python, focusing on custom URL converters to create dynamic URLs. It demonstrates how to handle URL parameters, default values, and data types, as well as custom error handling and templating.
Full Transcript
what's going on everybody welcome to another flash tutorial video in this video what we're going to be talking about is URL converters or kind of the way I see it as a way to make Dynamic URLs so the idea here is to take the URL path itself and treat each little path along the way as a variable or actually treat the entire path itself as a variable so obviously you know there are ways to pass variables through and get variables from the URL uh just natively through getting post commands for example uh but here it's a little more uh fluid way to handle the URLs but also it's going to save you some coding in certain scenarios so at least on on my end a certain scenario is like down here where you know I thought I would make custom tutorials per you know tutorial But as time goes on you start to really template things and what we're seeing here is very obvious I mean we've got functions that pretty much for the most part are identical functions some of the words might be a little different but at the end of the day these functions boil down to the same thing as the other ones doing so the dynamic URLs or the URL converters as they're called with flask uh really handles a situation like this very well so uh with that let's go ahead and hop to it and uh start an example so let's find where we had been working before so I'm searching Ginger Man and we'll take this function here copy paste now we've got our own I'm just making some space here a lot of times people ridicule me for my wh space I generally make the wh space so it's easier to see on the tutorial so um sorry if that really bothers you but uh it makes sense to kind of make the space on the tutorial so what we're going to do is we're going to we'll start off with a page called uh converters and the way that we do well first let's change the name of these functions uh so converter converter example and then the template we're going to return first of all let's get rid of this data and then the template we're going to return will be uh converter example. HTML oops we already had that part and we're not passing data so that's the page that we're going to visit now to have these URL converters or I they basically are tags that you treat at least you treat them like tags so an example of this might be maybe this conver is it's a a forum and you've got pages so you might have any URL a page like that so that's basically it's like a t tag and then every time you've got a little parameter variable whatever you want to call it here you take that and pass it through the function just like you would with an actual python function this is being passed through then finally you want to pass that through to the template if you're going to use it in the template but you actually you wouldn't necess it's not necessarily the ca going to be the case that every time you're going to pass everything through here through the function through to the template you might actually just have whatever this is changing the logic IC in the in the actual function itself and returning maybe a whole different template or something like that so uh but for now we're going to pass it through just so we can see the functionality work so we've got page equals page cool so we'll save that and now we need to make a converter example. HTML so we'll come into the templates and we'll duplicate 404 here uh and we'll call this uh con converter example. HTML sure we'll open the shell here and good to go converter example open that up and here we go so let me uh let's clean this up just like we cleaned up the other ones before div to div done all right so now for now we'll just reference the variable okay so we're just going to say oops uh page okay so we'll save that and that should really be it so let's go ahead and come over here and by the way um for some reason if you do service Apache to reload and you're not on the root user that's what was causing the problem if I do pseudo service Apache to reload um that seems to actually reload things I I can't imagine that restarting Apache with pseudo or not pseudo I can't believe that actually causes a difference because I mean you're you're you're both referencing the exact same service uh but apparently that does make a difference so or at least from what I've found so far so any anyway keep that in mind now uh what we're going to do is head to our website and instead of dashboard let's go to well first let's go to converters and then add a page here page five okay uh and so we're grabbing five at least at this point but what happens if you just wanted to go to like uh converters you're going to get a 404 because converters is not actually being addressed in any of our uh code here so uh what happened to my in it is it still up yeah so right now we're requiring there to be converters and then the page but there's really no addressing just simply converters so generally I mean you wouldn't want to have to like you would probably want to have just a page here and then you start populating the page numbers if if you need them right you might not actually need them and it just adds more to the URL so instead you'd probably want to do something like this like app rot converters close that off cool so then you might try to do something like that but you're going to find out that doesn't work and we come over here refresh and we get basically a 500 this is a custom 500 error but uh you'll get a 500 what went wrong well uh we have nothing for page we're trying to pass page we have nothing for page uh so what we want to do is set a default parameter right so easy enough save that come back over here restart come back to uh here here refresh and sure not if we're on the default page which is page one now uh we're trying to say hey these are page numbers but what happens if you instead of uh a page you put uh G okay uh we're going to get a g but that's not an an actual page so so what can we do there uh to handle for G when we really want that to be a page uh a page number well page numbers are all integers so you would use int so this basically requires it to be an INT if you pass something that is not an INT through to here uh it's not going to work right because we have no handling for a string and not an in so for example let's reload refresh we get a 404 but if we pass a five here we get the five and no other problems so then uh what if for example ex Le five works but what if we did a 6.0 we get a 404 6.0 is convertible to an INT but that's not an INT and we have this little dot here and that's what's causing some of the trouble but we can do float so we can do that restart come over here refresh and sure enough we get a 6.0 okay uh another one that we can do is string so so for example um if we started saying like python is so cool and we came over here and we said hey we we won't take anything let's just do the regular page let's say cuz that was working right for most things so we do that uh you'll see that we actually we get python is so cool and we even get our little exclamation mark and uh and so like that basically works it's a lot like the the default version basically that you're going to have um but if you wanted to use purely string you would say something like this you could just say string page now the problem with the string page is going to be the following so string takes everything but a slash so what if you wanted to do something like um python is cool okay like that was your path um or let's just something more realistic so python tutorials uh Basics okay you wanted to handle for a path like that uh we could go but we see that's we're we're having some sort of issue here uh and we we could try to say hey this is Page uh we could refresh there come over here still we don't have any handling for for this but what we can do is instead of page uh you can say path uh and then we'll say we'll call it page for now we should probably call it URL path or something but uh let's reload come over here uh refresh and there you go you've got the actual path python SL tutorialsbasic so we pick up all of that information so that's pretty useful because it allows you basically to uh to have a b a very Dynamic path so you might have tutorials Basics uh and that's like the basics tutorials maybe they want take it even further and they want to go to uh wow Loop or something like that um you you would pick up that right any length of path now uh another thing that you might do is like for example you might have uh converters and then you might have string and then article and then here or actually maybe maybe it's a string and this is your thread title and then you have an uh int uh that is a page okay so we need to pass uh the thread and we need to pass it down here too so thread equals thread save that we'll come back over here and let's just add the uh thread here thread and let's um let's put it put it at least this and paragraph tag so they're on a separate line save we'll come over here we'll reload and go back over here and we'll say the thread will be python the page will be uh 15 didn't work let's see what happened coming back over here so thread is a string that should have been fine int was a page we pass thread page thread page let's refresh one more time here python 15 still not working now what's wrong with you okay fine we'll come back over here that went through let's see Converse string [Music] thread let's try to give this a default just to see thread page for example I'm sure it's like a super obvious problem here but I'm not seeing it where is let's move this dang it let's refresh that so we're not get for some reason we're not getting it what is happening do we not have the okay I see what's happening maybe uh maybe it's that I don't that shouldn't cause the problem really it would be like if you didn't have the slash wow usually that doesn't cause the problem like okay so so what happened there was if you have an an app rout that uh has a slash like this like slash converter slash uh you can typically go to the website yeah okay so I guess maybe that was what was causing our problem so if you have a slash and then converters and then slash you can visit that URL by by either having slash converter slash or just slash converters and it will just be automatically converted to having a slash but if you don't have a slash the only way you can code that URL is with Slash converters no slash so uh so that was what was causing us some trouble there uh we did not have the slash here so we had the Slash and the Slash and we come back over here reload uh that should still work and then it probably impose a slash yeah so it adds the slash there but you're capable of of entering without the slash okay so that was the problem so so that and this case you're basically requiring there be a string and an imp page and the other one was a path where you could have basically unlimited um you know paths now uh what's interesting about this is is actually you could use something like this especially the path to actually make your entire website all contained within one function now that doesn't mean you should do that it just means that you can do that so for example if we go to the very very top here and find find let's just do uh let's just go to the see if we can find there it is so this is our main this is like our homepage right so how would we get to the homepage uh with a homepage and then handle for any other path so first what you would do is you would handle for uh the slash okay then what you would do is you're going to handle for Slash and then a tag so so like let's go take this slash and then handle for path uh URL path and then don't forget your little slash there and then URL path we'll say the default URL path is a slash and we still need the slash for the reasons that we needed it before don't forget that part uh so we still need this line and I think that's all we need let's let's try that and and basically any page even if it would in theory have previously resulted in a 404 any page will actually result in the homepage so uh let's let's refresh here or reload and so here is our website let's go ahead and reload here make sure we don't have any error dashboard cool let's try to go to the homepage no error on the homepage and then uh let's go to this web there sure enough that takes us to the homepage so now any link would take us to the homepage so well any link that isn't uh explicitly addressed in the init.py anyway so if we go to you know converters 5 for example that should oh that's actually taking us to the well we don't have any handling for converters 5 so that's exact that's our problem but if we go uh like we go to the dashboard for example uh let's go here let's go to the homepage and then you go to dashboard that's explicitly defined in our init.py so so we're able to go there but if you went to like dashboard uh this and then this and then this that's not defined explicitly but it is handled for uh in this so that's pretty cool you could have everything in that main function you could have like a you know if if you were like a really good programmer you could probably have everything in the main function and still have it be a pretty nice clean website and a really short main function so you could probably some pretty cool stuff there but uh that's not me but anyway uh that's just to kind of show that you can have how how actually powerful these Dynamic URLs or URL converters actually are so anyways uh that's it for this tutorial if you have questions comments concerns I'm not sure what concerns you might have but if you did have them you can leave them below uh the next tutorial we're going to be talking about flask mail which is a pretty powerful mailing application that's kind of built around flask it's really easy to use even if you weren't using flask for a website I would use flask mail because it's so simple I would just make a quick app that that just issues mail uh through flask cuz I'm pretty happy with this flask mail I Haven found another application that does it as well as flash mail so anyways that's what we'll be talking about in the next tutorial so stay tuned for that uh anyway till next time
Original Description
Welcome to part 28 of our Flask web development tutorial series. In this tutorial, we're going to be discussing custom converters. The idea of these custom converters is to allow us to create very dynamic URLs, where part of the URL is actually treated as a variable.
An example of where this may prove useful might be where you have something with pagination, or maybe a user account page. Another example would be with PythonProgramming.net's development version content management system, where we have lots of pages that are almost identical, where the only main difference in the function itself is the URL and the template that is rendered, along with some minor variable changes along the way. Let's start with a simple example of a converter.
Text-based tutorial and sample code for this tutorial: http://pythonprogramming.net/flask-url-converters-tutorial/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Dev.to · IAMUU
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
🎓
Tutor Explanation
DeepCamp AI