Documented some routing converter possibilities.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 5acc491 · Browse files · 3 files·+36 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+36 added−6 removed
- tests:+16 added−1 removed
- other:+20 added−5 removed
Provenance
- Commit
95750b3- Tree
3f63411- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5acc491
3 files changed+36 added−6 removed
modified:docs/api.rst+1 added-1 removed
104 unmodified lines105 `path` ``/page.html``106 `script_root` ``/myapplication``107 `url` ``http://www.example.com/myapplication/page.html``108 `base_url` ``http://www.example.com/myapplication/page.html?x=y``109- `root_url` ``http://www.example.com/myapplication/``109+ `url_root` ``http://www.example.com/myapplication/``110 ============= ======================================================111 112 .. attribute:: is_xhr113 149 unmodified linesmodified:tests/flask_tests.py+16 added-1 removed
61 unmodified lines62 def index():63 return flask.request.method64 def more():65 return flask.request.method66- 66+67 app.add_url_rule('/', 'index', index)68 app.add_url_rule('/more', 'more', more, methods=['GET', 'POST'])69 70 c = app.test_client()109 unmodified lines180 pass181 with app.test_request_context():182 assert flask.url_for('hello', name='test x') == '/hello/test%20x'183 184+ def test_custom_converters(self):185+ from werkzeug.routing import BaseConverter186+ class ListConverter(BaseConverter):187+ def to_python(self, value):188+ return value.split(',')189+ def to_url(self, value):190+ return ','.join(super(ListConverter, self).to_url(x) for x in value)191+ app = flask.Flask(__name__)192+ app.url_map.converters['list'] = ListConverter193+ @app.route('/<list:args>')194+ def index(args):195+ return '|'.join(args)196+ c = app.test_client()197+ assert c.get('/1,2,3').data == '1|2|3'198+199 def test_static_files(self):200 app = flask.Flask(__name__)201 rv = app.test_client().get('/static/index.html')202 assert rv.status_code == 20079 unmodified linesmodified:flask.py+19 added-4 removed
14 unmodified lines15 16 from jinja2 import Environment, PackageLoader, FileSystemLoader17 from werkzeug import Request as RequestBase, Response as ResponseBase, \18 LocalStack, LocalProxy, create_environ, SharedDataMiddleware, \19- cached_property19+ ImmutableDict, cached_property20 from werkzeug.routing import Map, Rule21 from werkzeug.exceptions import HTTPException22 from werkzeug.contrib.securecookie import SecureCookie23 281 unmodified lines305 #: The secure cookie uses this for the name of the session cookie306 session_cookie_name = 'session'307 308 #: options that are passed directly to the Jinja2 environment309- jinja_options = dict(309+ jinja_options = ImmutableDict(310 autoescape=True,311 extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_']312 )313 46 unmodified lines360 #: To register a function here, use the :meth:`context_processor`361 #: decorator.362 self.template_context_processors = [_default_template_ctx_processor]363 364+ #: the :class:`~werkzeug.routing.Map` for this instance. You can use365+ #: this to change the routing converters after the class was created366+ #: but before any routes are connected. Example::367+ #:368+ #: from werkzeug import BaseConverter369+ #:370+ #: class ListConverter(BaseConverter):371+ #: def to_python(self, value):372+ #: return value.split(',')373+ #: def to_url(self, values):374+ #: return ','.join(BaseConverter.to_url(value)375+ #: for value in values)376+ #:377+ #: app = Flask(__name__)378+ #: app.url_map.converters['list'] = ListConverter379 self.url_map = Map()380 381 if self.static_path is not None:367- self.url_map.add(Rule(self.static_path + '/<filename>',368- build_only=True, endpoint='static'))382+ self.add_url_rule(self.static_path + '/<filename>',383+ build_only=True, endpoint='static')384 if pkg_resources is not None:385 target = (self.package_name, 'static')386 else:387 target = os.path.join(self.root_path, 'static')400 unmodified lines