Flask,如何返回值,处理后从输入表中获取数据

时间:2016-01-31 15:25:53

标签: python web-applications flask flask-wtforms

我尝试使用Flask创建webapp;

dicrectory tree是这样的:

online_service:
----应用:
--------forms.py
--------的初始化的.py
--------模板:
------------的index.html
------------的login.html
--------views.py
----run.py

和: run.py:

import os
import sys 
from app import app 
app.config['SECRET_KEY'] = 'any string'
app.run(host='0.0.0.0', port = 8889)

views.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
from flask import render_template, flash, redirect
from app import app
from .forms import LoginForm

file_dir = os.path.split(os.path.realpath(__file__))[0]
os.chdir(file_dir)
cur_dir = os.getcwd()
back_end_dir = os.path.join(cur_dir, "./../../back_end")
sys.path.append(back_end_dir)
import main

@app.route('/')
@app.route('/index')

def index():
    return render_template("index.html",
        title = 'Home')

def process(url):
    url_status = "Invalid"
    res = ""           
    res = main.get_life_stage(url)
    url_str = "Url: " + url
    url_status = "valid"
    url_status_str = "Url_status: " + url_status
    ret_str = "\n".join([url_str, url_status_str, res])
    return ret_str


@app.route('/search', methods = ['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        url = form.openid.data
        ret_str = ""
        ret = process(url)
        flash(ret_str)
        return redirect('/index')
    return render_template('login.html',
        title = 'Sign In',
        form = form)

在views.py中,ret = process(url)处理来自前面的数据,然后返回一个字符串,ret = process(url)在单元测试中运行良好,但在视图中不起作用

那么,如何显示进程函数的返回字符串? 任何提示,我都会感激。 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你应该分配给/** * Throw TypeError when val is invalid * @param {JOIN_TYPES} type * @param {Class|Function} cls * @param {*} val * @param {Object} options * @param {String} options.wrongJoinTypeErrorMessage * @param {Boolean} options.nullable */ function validateJoinSet(type, cls, val, options) { //... }

 * @param {Class|Function} cls

因为您使用ret_str

-

我希望您在ret_str = process(url)

的模板中使用此闪烁消息