Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url_for method for simple routes, blueprints, HTTPMethodView #387

Merged
merged 6 commits into from
Feb 9, 2017

Conversation

subyraman
Copy link
Contributor

@subyraman subyraman commented Feb 2, 2017

Adds in a url_for method to generate URLs based on an endpoint's handler name, similar to Flask. Provides the same type of functionality as Flask, but will additionally fail URL building if a supplied parameter does not pass the request parameter pattern. From the docs I wrote:

Sanic provides a url_for method, to generate URLs based on the handler method name. This is useful if you want to avoid hardcoding url paths into your app; instead, you can just reference the handler name. For example:

@app.route('/')
async def index(request):
    # generate a URL for the endpoint `post_handler`
    url = app.url_for('post_handler', post_id=5)
    # the URL is `/posts/5`, redirect to it
    return redirect(url)


@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
    return text('Post - {}'.format(post_id))

Other things to keep in mind when using url_for:

  • Keyword arguments passed to url_for that are not request parameters will be included in the URL's query string. For example:
url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
# /posts/5?arg_one=one&arg_two=two
  • All valid parameters must be passed to url_for to build a URL. If a parameter is not supplied, or if a parameter does not match the specified type, a URLBuildError will be thrown.

Caveats: I did not try to make this work with static files or the CompositionView. The reason is I found no logical pattern for getting a view name for static handlers, since there is no unique "handler" surrounding a static file. Same for CompositionView handlers.

One possible idea would be to force the naming of a class based view, like Flask does:

app.add_route(SpecialClassView.as_view('special_class_name'), '/special_class_view')

Another idea would be to make the static file API similar to routing, with unique handlers:

@app.static(/static/images/<image_file>)
def image_file():
   ...

But these were out of scope for this request.

Fixes #336

@subyraman subyraman closed this Feb 3, 2017
@subyraman
Copy link
Contributor Author

There's some issues with the CompositionView I want to clear up.

@subyraman subyraman reopened this Feb 6, 2017
@subyraman
Copy link
Contributor Author

Reopening this; the CompositionView testing issues are unrelated to this branch. Going to file another PR to add tests for the CompositionView (it has none, currently)

Copy link
Contributor

@r0fls r0fls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit shouldn't be included: aa54785

Copy link
Member

@seemethere seemethere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As always, super awesome work @subyraman

@seemethere seemethere added this to the 0.3.1 milestone Feb 9, 2017
@seemethere seemethere merged commit 6724d81 into sanic-org:master Feb 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants