尝试路由移动,但会引发ValueError

时间:2015-03-08 02:12:52

标签: python mobile flask

我正在开发一个需要移动友好的Flask网站,我找到了一个很好的移动UA检测代码。只是为了测试,我创建了一个路由,如果移动,将路由我的404页面,如果没有,则创建一个索引。我的代码如下:

# Main application file
# Created by James Parsons 2/23/15
from flask import *
from flask.ext.mobility import Mobility
from flask.ext.mobility.decorators import mobile_template
import re

def isMobile():
    browser = request.user_agent.browser
    version = request.user_agent.version and int(request.user_agent.version.split('.')[0])
    platform = request.user_agent.platform
    uas = request.user_agent.string

    if browser and version:
        if (browser == 'msie' and version < 9) \
        or (browser == 'firefox' and version < 4) \
        or (platform == 'android' and browser == 'safari' and version < 534) \
        or (platform == 'iphone' and browser == 'safari' and version < 7000) \
        or ((platform == 'macos' or platform == 'windows') and browser == 'safari' and not re.search('Mobile', uas) and version < 534) \
        or (re.search('iPad', uas) and browser == 'safari' and version < 7000) \
        or (platform == 'windows' and re.search('Windows Phone OS', uas)) \
        or (browser == 'opera') \
        or (re.search('BlackBerry', uas)):
            return True
    else:
        return False

app = Flask(__name__)

@app.route("/")
def index():
    if isMobile():
        render_template("404.html")
    else:
        return render_template("index.html")

@app.route("/about")
def about():
    return render_template("about.html")

@app.errorhandler(404)
def error_404(err):
    return render_template("404.html"), 404

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

但是当我使用Chromes UA欺骗时,我得到了:

builtins.ValueError
ValueError: View function did not return a response

Traceback (most recent call last)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python34\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python34\lib\site-packages\flask\app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1566, in make_response
    raise ValueError('View function did not return a response')
ValueError: View function did not return a response

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您忘记返回 ifMobile()分支中的呈现模板:

if isMobile():
    render_template("404.html")
    # No return, so the result is ignored

您最终会返回None

添加return声明:

if isMobile():
    return render_template("404.html")

如果您确实想要返回HTTP 404 Not Found 响应,则还需要包含状态代码:

if isMobile():
    return render_template("404.html"), 404

或者,使用abort() function引发相应的异常,并让Flask根据该视图返回404视图:

from flask import abort

if isMobile():
    abort(404)