Skip to content
Myr

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

2 files+32 added−12 removed

modified:docs/patterns/jquery.rst+8 added-1 removed
86 unmodified lines
87Now let's create a server side function that accepts two URL arguments of88numbers which should be added together and then sent back to the89application in a JSON object. This is a really ridiculous example and is90something you usually would do on the client side alone, but a simple91example that shows how you would use jQuer and Flask nonetheless::91example that shows how you would use jQuery and Flask nonetheless::9293 from flask import Flask, jsonify, render_template, request94 app = Flask(__name__)95
11 unmodified lines
107template. This template will load jQuery as above and have a little form108we can add two numbers and a link to trigger the function on the server109side.110111Note that we are using the :meth:`~werkzeug.MultiDict.get` method here112which will never fail. If the key is missing a default value (here ``0``)113is returned. Furthermore it can convert values to a specific type (like114in our case `int`). This is especially handy for code that that is115triggered by a script (APIs, JavaScript etc.) because you don't need116special error reporting in that case.117118The HTML119--------120121You index.html template either has to extend a `layout.html` template with
46 unmodified lines
modified:flask.py+24 added-11 removed
64 unmodified lines
65 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)7374
134 unmodified lines
209 This requires Python 2.6 or an installed version of simplejson.210211 .. 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')217218
33 unmodified lines
252 g=reqctx.g253 )254255256def _assert_have_json():257 """Helper function that fails if JSON is unavailable"""258 if not json_available:259 raise RuntimeError('simplejson not installed')260261262def _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 lines
268269270def _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('</', '<\\/')275276277class Flask(object):
129 unmodified lines
407 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_filter412413 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 the
90 unmodified lines
506 session.save_cookie(response, self.session_cookie_name)507508 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.512513 Basically this example::514515 @app.route('/')
4 unmodified lines
520521 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:524525 If the view_func is not provided you will need to connect the endpoint526 to a view function like so::527528 app.view_functions['index'] = index529530 .. versionchanged:: 0.2531 `view_func` parameter added532533 :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` object526527 .. versionadded:: 0.2541 """542 options['endpoint'] = endpoint543 options.setdefault('methods', ('GET',))544 self.url_map.add(Rule(rule, **options))
256 unmodified lines