Documented some routing converter possibilities.
This commit is contained in:
parent
5acc491c94
commit
95750b3287
3 changed files with 36 additions and 6 deletions
|
|
@ -106,7 +106,7 @@ Incoming Request Data
|
|||
`script_root` ``/myapplication``
|
||||
`url` ``http://www.example.com/myapplication/page.html``
|
||||
`base_url` ``http://www.example.com/myapplication/page.html?x=y``
|
||||
`root_url` ``http://www.example.com/myapplication/``
|
||||
`url_root` ``http://www.example.com/myapplication/``
|
||||
============= ======================================================
|
||||
|
||||
.. attribute:: is_xhr
|
||||
|
|
|
|||
23
flask.py
23
flask.py
|
|
@ -16,7 +16,7 @@ import sys
|
|||
from jinja2 import Environment, PackageLoader, FileSystemLoader
|
||||
from werkzeug import Request as RequestBase, Response as ResponseBase, \
|
||||
LocalStack, LocalProxy, create_environ, SharedDataMiddleware, \
|
||||
cached_property
|
||||
ImmutableDict, cached_property
|
||||
from werkzeug.routing import Map, Rule
|
||||
from werkzeug.exceptions import HTTPException
|
||||
from werkzeug.contrib.securecookie import SecureCookie
|
||||
|
|
@ -306,7 +306,7 @@ class Flask(object):
|
|||
session_cookie_name = 'session'
|
||||
|
||||
#: options that are passed directly to the Jinja2 environment
|
||||
jinja_options = dict(
|
||||
jinja_options = ImmutableDict(
|
||||
autoescape=True,
|
||||
extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_']
|
||||
)
|
||||
|
|
@ -361,11 +361,26 @@ class Flask(object):
|
|||
#: decorator.
|
||||
self.template_context_processors = [_default_template_ctx_processor]
|
||||
|
||||
#: the :class:`~werkzeug.routing.Map` for this instance. You can use
|
||||
#: this to change the routing converters after the class was created
|
||||
#: but before any routes are connected. Example::
|
||||
#:
|
||||
#: from werkzeug import BaseConverter
|
||||
#:
|
||||
#: class ListConverter(BaseConverter):
|
||||
#: def to_python(self, value):
|
||||
#: return value.split(',')
|
||||
#: def to_url(self, values):
|
||||
#: return ','.join(BaseConverter.to_url(value)
|
||||
#: for value in values)
|
||||
#:
|
||||
#: app = Flask(__name__)
|
||||
#: app.url_map.converters['list'] = ListConverter
|
||||
self.url_map = Map()
|
||||
|
||||
if self.static_path is not None:
|
||||
self.url_map.add(Rule(self.static_path + '/<filename>',
|
||||
build_only=True, endpoint='static'))
|
||||
self.add_url_rule(self.static_path + '/<filename>',
|
||||
build_only=True, endpoint='static')
|
||||
if pkg_resources is not None:
|
||||
target = (self.package_name, 'static')
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class BasicFunctionalityTestCase(unittest.TestCase):
|
|||
return flask.request.method
|
||||
def more():
|
||||
return flask.request.method
|
||||
|
||||
|
||||
app.add_url_rule('/', 'index', index)
|
||||
app.add_url_rule('/more', 'more', more, methods=['GET', 'POST'])
|
||||
|
||||
|
|
@ -181,6 +181,21 @@ class BasicFunctionalityTestCase(unittest.TestCase):
|
|||
with app.test_request_context():
|
||||
assert flask.url_for('hello', name='test x') == '/hello/test%20x'
|
||||
|
||||
def test_custom_converters(self):
|
||||
from werkzeug.routing import BaseConverter
|
||||
class ListConverter(BaseConverter):
|
||||
def to_python(self, value):
|
||||
return value.split(',')
|
||||
def to_url(self, value):
|
||||
return ','.join(super(ListConverter, self).to_url(x) for x in value)
|
||||
app = flask.Flask(__name__)
|
||||
app.url_map.converters['list'] = ListConverter
|
||||
@app.route('/<list:args>')
|
||||
def index(args):
|
||||
return '|'.join(args)
|
||||
c = app.test_client()
|
||||
assert c.get('/1,2,3').data == '1|2|3'
|
||||
|
||||
def test_static_files(self):
|
||||
app = flask.Flask(__name__)
|
||||
rv = app.test_client().get('/static/index.html')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue