Advanced path parameters
In the last section, we learned the basics of extracting information from a dynamic URL path to something we can code with. This is truly a fundamental feature of all web frameworks. It is also extremely common among many frameworks to allow you to specify what that path parameter should be. We learned that /messages/<message_id:int>
would match /messages/123
but not /messages/abc
. We also learned about the convenience that Sanic provides in converting the match path segment to an integer.
But what about more complex types? Or what if we need to modify the matched value before using it in our application? In this section, we will explore a couple of helpful patterns to achieve these goals.
Custom parameter matching
Out of the box, Sanic provides eight path parameter types that can be matched:
str
: Matches any valid stringslug
: Matches standard path slugsint
: Matches any integerfloat
: Matches any numberalpha
: Matches...