Browse Source

Some more documentation updates.

pull/1638/head
Armin Ronacher 15 years ago
parent
commit
715f64d2af
  1. 9
      docs/patterns/jquery.rst
  2. 35
      flask.py

9
docs/patterns/jquery.rst

@ -88,7 +88,7 @@ Now let's create a server side function that accepts two URL arguments of
numbers which should be added together and then sent back to the
application in a JSON object. This is a really ridiculous example and is
something you usually would do on the client side alone, but a simple
example that shows how you would use jQuer and Flask nonetheless::
example that shows how you would use jQuery and Flask nonetheless::
from flask import Flask, jsonify, render_template, request
app = Flask(__name__)
@ -108,6 +108,13 @@ template. This template will load jQuery as above and have a little form
we can add two numbers and a link to trigger the function on the server
side.
Note that we are using the :meth:`~werkzeug.MultiDict.get` method here
which will never fail. If the key is missing a default value (here ``0``)
is returned. Furthermore it can convert values to a specific type (like
in our case `int`). This is especially handy for code that that is
triggered by a script (APIs, JavaScript etc.) because you don't need
special error reporting in that case.
The HTML
--------

35
flask.py

@ -66,8 +66,8 @@ class Request(RequestBase):
"""If the mimetype is `application/json` this will contain the
parsed JSON data.
"""
if not json_available:
raise AttributeError('simplejson not available')
if __debug__:
_assert_have_json()
if self.mimetype == 'application/json':
return json.loads(self.data)
@ -210,6 +210,8 @@ def jsonify(*args, **kwargs):
.. versionadded:: 0.2
"""
if __debug__:
_assert_have_json()
return current_app.response_class(json.dumps(dict(*args, **kwargs),
indent=None if request.is_xhr else 2), mimetype='application/json')
@ -251,6 +253,12 @@ def _default_template_ctx_processor():
)
def _assert_have_json():
"""Helper function that fails if JSON is unavailable"""
if not json_available:
raise RuntimeError('simplejson not installed')
def _get_package_path(name):
"""Returns the path to a package or cwd if that cannot be found."""
try:
@ -261,6 +269,8 @@ def _get_package_path(name):
def _tojson_filter(string, *args, **kwargs):
"""Calls dumps for the template engine, escaping Slashes properly."""
if __debug__:
_assert_have_json()
return json.dumps(string, *args, **kwargs).replace('</', '<\\/')
@ -398,8 +408,7 @@ class Flask(object):
url_for=url_for,
get_flashed_messages=get_flashed_messages
)
if json_available:
self.jinja_env.filters['tojson'] = _tojson_filter
self.jinja_env.filters['tojson'] = _tojson_filter
def create_jinja_loader(self):
"""Creates the Jinja loader. By default just a package loader for
@ -498,7 +507,8 @@ class Flask(object):
def add_url_rule(self, rule, endpoint, view_func=None, **options):
"""Connects a URL rule. Works exactly like the :meth:`route`
decorator. If a view_func is provided it will be registered with the endpoint.
decorator. If a view_func is provided it will be registered with the
endpoint.
Basically this example::
@ -511,20 +521,23 @@ class Flask(object):
def index():
pass
app.add_url_rule('/', 'index', index)
If the view_func is not provided you will need to connect the endpoint to a
view function like so:
If the view_func is not provided you will need to connect the endpoint
to a view function like so::
app.view_functions['index'] = index
.. versionchanged:: 0.2
`view_func` parameter added
:param rule: the URL rule as string
:param endpoint: the endpoint for the registered URL rule. Flask
itself assumes the name of the view function as
endpoint
:param view_func: the function to call when servicing a request to the provided endpoint
:param view_func: the function to call when servicing a request to the
provided endpoint
:param options: the options to be forwarded to the underlying
:class:`~werkzeug.routing.Rule` object
.. versionadded:: 0.2
"""
options['endpoint'] = endpoint
options.setdefault('methods', ('GET',))

Loading…
Cancel
Save