diff --git a/docs/routes.md b/docs/routes.md index 6a43761..ee53a27 100644 --- a/docs/routes.md +++ b/docs/routes.md @@ -153,7 +153,7 @@ As with the `routes` argument, as many routes as you'd like can be registered he ## Finding Routes Again -> New in 1.1.0 +> New in 1.1.0! If you need to find the path that's associated with a route (for example, for a RedirectResponse), you can use the `app.reverse()` function to find it. This function takes the name of the view and returns the path that it's associated with. For example: @@ -166,14 +166,22 @@ path = app.reverse("example") print(path) # -> "/example" ``` -If you have a route that takes arguments, you can pass them in as keyword arguments: +If you have a route that takes arguments, you can pass them in as a dictionary: ```python -@app.route("/example/", name="example") -def example(request, id): - return HttpResponse(body=f"Example with id {id}") +@app.route("/example/", name="example") +def example(request, obj_id): + return HttpResponse(body=f"Example with id {obj_id}") -path = app.reverse("example", id=3) +path = app.reverse("example", {'obj_id': 3}) print(path) # -> "/example/3" ``` + +You can also provide a dictionary of query parameters to be added to the URL: + +```python +path = app.reverse("example", {'obj_id': 3}, query={'name': 'james'}) +print(path) # -> "/example/3?name=james" +``` + The arguments you pass in must match what the path expects, or you'll get a `SpiderwebException`. If there's no route with that name, you'll get a `ReverseNotFound` exception instead. \ No newline at end of file