【问题标题】:uwsgi error log show flask TypeError: The view function did not return a valid response [duplicate]uwsgi错误日志显示flask TypeError:视图函数没有返回有效响应[重复]
【发布时间】:2018-09-05 10:47:51
【问题描述】:

我正在学习编写 python 烧瓶代码。 我的烧瓶应用程序使用 owm Web 服务器是可以的。但是,使用nginx+uwsgi+flask deploy,uwsgi报错:

    [2018-09-05 18:28:59,295] ERROR in app: Exception on /editor [GET]
Traceback (most recent call last):
  File "/usr/lib64/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib64/python3.6/site-packages/flask/app.py", line 1816, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/lib64/python3.6/site-packages/flask/app.py", line 1831, in finalize_request
    response = self.make_response(rv)
  File "/usr/lib64/python3.6/site-packages/flask/app.py", line 1957, in make_response
    'The view function did not return a valid response. The'
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

我的烧瓶代码是烧瓶示例。代码如下:

# coding=utf-8
"""
    ~~~~~~
    A microblog example application written as Flask tutorial with
    Flask and sqlite3.
    :copyright: (c) 2010 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
"""

from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash


# create our little application :)
app = Flask(__name__)

# Load default config and override config from an environment variable
app.config.update(dict(
    DATABASE='/tmp/flaskr.db',
    DEBUG=False,
    SECRET_KEY='development key',
    USERNAME='admin',
    PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)


def connect_db():
    print ("connect db")
    """Connects to the specific database."""
    rv = sqlite3.connect(app.config['DATABASE'])
    rv.row_factory = sqlite3.Row
    return rv


def init_db():
    """Creates the database tables."""
    with app.app_context():
        db = get_db()
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
            print("excute sql")
        db.commit()


def get_db():
    """Opens a new database connection if there is none yet for the
    current application context.
    """
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
    return g.sqlite_db


@app.teardown_appcontext
def close_db(error):
    """Closes the database again at the end of the request."""
    if hasattr(g, 'sqlite_db'):
        g.sqlite_db.close()


@app.route('/')
def show_entries():
    db = get_db()
    cur = db.execute('select title, text from entries order by id desc')
    entries = cur.fetchall()
    return render_template('show_entries.html', entries=entries)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return redirect(url_for('show_entries'))

@app.route('/editor')
def editor():
    print ("editor")
    return render_template('index.html')

if __name__ == '__main__':
    init_db()
    app.run(host='0.0.0.0')

其他视图功能正常。只有编辑视图功能出错。但只有运行烧瓶是可以的。添加 nginx 和 uwsgi 是错误的。

【问题讨论】:

    标签: python flask uwsgi


    【解决方案1】:

    我重新启动了uswgi,编辑器视图功能起作用了。可能是因为 uwsgi 无法检测到 python 代码的变化。

    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多