Some more documentation updates.
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 1404a85 · Browse files · 2 files·+32 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−12 removed
- other:+32 added−12 removed
Provenance
- Commit
715f64d- Tree
62125fd- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1404a85
2 files changed+32 added−12 removed
modified:docs/patterns/jquery.rst+8 added-1 removed
86 unmodified lines87 Now let's create a server side function that accepts two URL arguments of88 numbers which should be added together and then sent back to the89 application in a JSON object. This is a really ridiculous example and is90 something you usually would do on the client side alone, but a simple91-example that shows how you would use jQuer and Flask nonetheless::91+example that shows how you would use jQuery and Flask nonetheless::92 93 from flask import Flask, jsonify, render_template, request94 app = Flask(__name__)95 11 unmodified lines107 template. This template will load jQuery as above and have a little form108 we can add two numbers and a link to trigger the function on the server109 side.110 111+Note that we are using the :meth:`~werkzeug.MultiDict.get` method here112+which will never fail. If the key is missing a default value (here ``0``)113+is returned. Furthermore it can convert values to a specific type (like114+in our case `int`). This is especially handy for code that that is115+triggered by a script (APIs, JavaScript etc.) because you don't need116+special error reporting in that case.117+118 The HTML119 --------120 121 You index.html template either has to extend a `layout.html` template with46 unmodified linesmodified:flask.py+24 added-11 removed
64 unmodified lines65 def json(self):66 """If the mimetype is `application/json` this will contain the67 parsed JSON data.68 """69- if not json_available:70- raise AttributeError('simplejson not available')69+ if __debug__:70+ _assert_have_json()71 if self.mimetype == 'application/json':72 return json.loads(self.data)73 74 134 unmodified lines209 This requires Python 2.6 or an installed version of simplejson.210 211 .. versionadded:: 0.2212 """213+ if __debug__:214+ _assert_have_json()215 return current_app.response_class(json.dumps(dict(*args, **kwargs),216 indent=None if request.is_xhr else 2), mimetype='application/json')217 218 33 unmodified lines252 g=reqctx.g253 )254 255 256+def _assert_have_json():257+ """Helper function that fails if JSON is unavailable"""258+ if not json_available:259+ raise RuntimeError('simplejson not installed')260+261+262 def _get_package_path(name):263 """Returns the path to a package or cwd if that cannot be found."""264 try:265 return os.path.abspath(os.path.dirname(sys.modules[name].__file__))2 unmodified lines268 269 270 def _tojson_filter(string, *args, **kwargs):271 """Calls dumps for the template engine, escaping Slashes properly."""272+ if __debug__:273+ _assert_have_json()274 return json.dumps(string, *args, **kwargs).replace('</', '<\\/')275 276 277 class Flask(object):129 unmodified lines407 self.jinja_env.globals.update(408 url_for=url_for,409 get_flashed_messages=get_flashed_messages410 )401- if json_available:402- self.jinja_env.filters['tojson'] = _tojson_filter411+ self.jinja_env.filters['tojson'] = _tojson_filter412 413 def create_jinja_loader(self):414 """Creates the Jinja loader. By default just a package loader for415 the configured package is returned that looks up templates in the90 unmodified lines506 session.save_cookie(response, self.session_cookie_name)507 508 def add_url_rule(self, rule, endpoint, view_func=None, **options):509 """Connects a URL rule. Works exactly like the :meth:`route`501- decorator. If a view_func is provided it will be registered with the endpoint.510+ decorator. If a view_func is provided it will be registered with the511+ endpoint.512 513 Basically this example::514 515 @app.route('/')4 unmodified lines520 521 def index():522 pass523 app.add_url_rule('/', 'index', index)514- 515- If the view_func is not provided you will need to connect the endpoint to a 516- view function like so:524+525+ If the view_func is not provided you will need to connect the endpoint526+ to a view function like so::527+528 app.view_functions['index'] = index529 530+ .. versionchanged:: 0.2531+ `view_func` parameter added532+533 :param rule: the URL rule as string534 :param endpoint: the endpoint for the registered URL rule. Flask535 itself assumes the name of the view function as536 endpoint523- :param view_func: the function to call when servicing a request to the provided endpoint537+ :param view_func: the function to call when servicing a request to the538+ provided endpoint539 :param options: the options to be forwarded to the underlying540 :class:`~werkzeug.routing.Rule` object526-527- .. versionadded:: 0.2541 """542 options['endpoint'] = endpoint543 options.setdefault('methods', ('GET',))544 self.url_map.add(Rule(rule, **options))256 unmodified lines