Flask 简明教程

Flask – Sijax

Sijax 代表 ‘Simple Ajax’ ,它是 Python/jQuery 库,旨在帮助您轻松地将 Ajax 引入到您的应用程序中。它使用 jQuery.ajax 发起 AJAX 请求。

Sijax stands for ‘Simple Ajax’ and it is a Python/jQuery library designed to help you easily bring Ajax to your application. It uses jQuery.ajax to make AJAX requests.

Installation

安装 Flask-Sijax 非常简单。

Installation of Flask-Sijax is easy.

pip install flask-sijax

Configuration

  1. SIJAX_STATIC_PATH − the static path where you want the Sijax javascript files to be mirrored. The default location is static/js/sijax. In this folder, sijax.js and json2.js files are kept.

  2. SIJAX_JSON_URI − the URI to load the json2.js static file from

Sijax 使用 JSON 在浏览器和服务器之间传递数据。这意味着浏览器要么需要原生支持 JSON ,要么通过 json2.js 文件获取 JSON 支持。

Sijax uses JSON to pass the data between the browser and the server. This means that the browsers need either to support JSON natively or get JSON support from the json2.js file.

那样注册的函数无法提供 Sijax 功能,因为默认情况下无法通过 POST 方法访问它们(而 Sijax 使用 POST 请求)。

Functions registered that way cannot provide Sijax functionality, because they cannot be accessed using a POST method by default (and Sijax uses POST requests).

要使 View 函数能够处理 Sijax 请求,可通过 POST 使用 @app.route('/url', methods = ['GET', 'POST']) 访问它,或者像这样使用 @flask_sijax.route 帮助器装饰器 −

To make a View function capable of handling Sijax requests, make it accessible via POST using @app.route('/url', methods = ['GET', 'POST']) or use the @flask_sijax.route helper decorator like this −

@flask_sijax.route(app, '/hello')

每个 Sijax 处理程序函数(如这个)都会至少自动接收一个参数,就像 Python 将“self”传递给对象方法一样。 ‘obj_response’ 参数是函数向浏览器返回响应的一种方式。

Every Sijax handler function (like this one) receives at least one parameter automatically, much like Python passes ‘self’ to the object methods. The ‘obj_response’ parameter is the function’s way of talking back to the browser.

def say_hi(obj_response):
   obj_response.alert('Hi there!')

当检测到 Sijax 请求时,Sijax 会像这样处理它 −

When Sijax request is detected, Sijax handles it like this −

g.sijax.register_callback('say_hi', say_hi)
   return g.sijax.process_request()

Sijax Application

一个最小的 Sijax 应用程序代码如下 −

A minimal Sijax application code looks as follows −

import os
from flask import Flask, g
from flask_sijax import sijax

path = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/')
app = Flask(__name__)

app.config['SIJAX_STATIC_PATH'] = path
app.config['SIJAX_JSON_URI'] = '/static/js/sijax/json2.js'
flask_sijax.Sijax(app)

@app.route('/')
def index():
   return 'Index'

@flask_sijax.route(app, '/hello')
def hello():
   def say_hi(obj_response):
      obj_response.alert('Hi there!')
   if g.sijax.is_sijax_request:
      # Sijax request detected - let Sijax handle it
      g.sijax.register_callback('say_hi', say_hi)
      return g.sijax.process_request()
      return _render_template('sijaxexample.html')

if __name__ == '__main__':
   app.run(debug = True)

当 Sijax 请求(一个特殊的 jQuery.ajax() 请求)发送到服务器时,这个请求会被服务器上的 g.sijax.is_sijax_request() 检测到,在这种情况下,您可以让 Sijax 处理该请求。

When a Sijax requests (a special jQuery.ajax() request) to the server, this request is detected on the server by g.sijax.is_sijax_request(), in which case you let Sijax handle the request.

使用 g.sijax.register_callback() 注册的所有函数都公开以便从浏览器调用。

All the functions registered using g.sijax.register_callback() are exposed for calling from the browser.

调用 g.sijax.process_request() 会指示 Sijax 执行适当的(先前注册的)函数并将响应返回至浏览器。

Calling g.sijax.process_request() tells Sijax to execute the appropriate (previously registered) function and return the response to the browser.