Parameters
Notes// archive From the original Code Walker docs. Kept for reference, so some versions and commands may be out of date.
At this stage, you should have a well-structured base project and a database configured with a table to keep blog post records.
Before writing code, it’s worth understanding the difference between Path Parameters and Query Parameters.
Path Parameters#
Path parameters are parameters that get included in the context path of a URL. For example, the unique ID of each blog post gets appended to the URL providing a link to individual posts.
https://www.exampleforyou.net/blog/963e4ca9-2080-4f15-93ba-a8509bb63f6bSo a corresponding function would be matched, and can utilize the value 963e4ca9-2080-4f15-93ba-a8509bb63f6b which becomes id from {id}, for example:
@router.get('/blog/{id}'...
def get_blogpost(*, id: UUID...Query Parameters#
A query a parameter is a key/value pair appended to a URL after a ?, for example:
https://www.exampleforyou.net/api/v1/blog?limit=2So the corresponding function doesn’t have any defined {} parameter in the path, but the function does expect an incoming value, in this case limit. (The syntax | None = None is new since Python 3.10):
@router.get('/blog'...
def get_blogposts(limit: int | None = None...Keep this in mind when working through the upcoming CRUD examples.