【问题标题】:ValueError: View function did not return a response in flask [duplicate]ValueError:查看函数在烧瓶中没有返回响应[重复]
【发布时间】:2015-01-07 07:09:10
【问题描述】:

所以已经提出了几个这样的问题,这个错误意味着函数必须返回一个值,或者只是意味着它应该返回一些东西。

我的 routes.py 文件中已经有了它,但它仍然无法正常工作。

这是 route.py 的代码

from flask import *
from functools import wraps
app = Flask(__name__)
app.secret_key = "my precious"

@app.route('/')
def home():
     return render_template('home.html')

@app.route('/welcome')
def welcome():
    return render_template('welcome.html')

@app.route('/logout')   
def logout():
    session.pop('logged_in',None)
    return redirect (url_for('home'))

@app.route('/hello')
def hello():
    return render_template('hello.html')


@app.route('/log', methods=['GET','POST'])
def log():
    error = None
    if request.method == "POST":
        if request.form['username'] != 'admin' or  request.form['password'] != 'admin':
            error = "Invalid credentials"
        else: 
            session['logged_in'] = True
            return redirect (url_for('hello'))
        return render_template('log.html', error=error)


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

log.html 代码

{% extends "templates.html" %}
{% block content %}
    <h1>Login</h1>
    {% If error %}
        <p class=error> <strong> Error: </strong> {{ error }}
    {% endif %}
    <form action="" method="POST">
        <dl>
            <dt>Username:
            <dt><input type="text" name="username" value="{{
            request.form.username }}">
            <dt>Password:
            <dd><input type="password" name="passowrd"> 
        </dl>
        <p><input type="submit" value="Login">      
    </form>
{% endblock %}

templates.html 的代码

<html>
<head>
        <title>Flask tutorial (Part 1)</title>
</head>
    <header>
    <div class="navbar navbar-inverse">
    <div class="navbar-inner">  
        <a class="brand" href="/">Real python (for the web!)</a>
        <ul>
            <li><a href="/welcome">Welcome</a></li>
            <li><a href="/log">login</a></li>
        </ul>
    </div>
    </div>
    </header>
<body>
    <div class="container">
        {% block content %}
        {% endblock %}
    </div>  
</body>
</html>

home.html 的代码

{% extends "templates.html"%}
{% block content %}
    <div class="jumbo">
        <h2>Welcome to Flask</h2>
        <br/>
        <p>click <a href="/welcome">here</a> to go to welcome page</p>
    </div>  
{% endblock %}  

hello.html 的代码

{% extends "templates.html" %}
{% block content %}
    <h2>Welcome! You are logged in.</h2>
{% endblock %}

welcome.html 的代码

{% extends "templates.html"%}
{% block content %}
    <h2>Sample</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
{% endblock %}  

有人可以帮我解决我哪里出错了吗? 提前致谢

【问题讨论】:

    标签: python flask


    【解决方案1】:

    如果您通过GET 访问log() 方法,它不会返回响应。 你的问题在错误发生的地方有点缺乏信息,但这是我可以推断的。

    编辑:查看视频,确实是log 方法给您带来了麻烦。 你不小心缩进太深了。

    @app.route('/log', methods=['GET','POST'])
    def log():
        error = None
        if request.method == "POST":
            if request.form['username'] != 'admin' or  request.form['password'] != 'admin':
                error = "Invalid credentials"
            else: 
                session['logged_in'] = True
                return redirect (url_for('hello'))
        return render_template('log.html', error=error)
    

    【讨论】:

    • 大家好,感谢您的回复,我从 methods=['GET','POST'] 中删除了 GET。我再次收到此错误:请求的 URL 不允许该方法。你能建议我该怎么做吗?这段代码来自我正在为烧瓶学习的教程
    • 如果没有您提供的更多信息,将很难进一步帮助您。有用的信息通常是你在哪里得到错误,你能发布吗?你用的是什么教程?
    • 嗨!谢谢,我正在浏览来自 youtube 的视频教程:youtube.com/watch?v=WCpNvteLCDI(目前在视频 3 上,已经完成了第一和第二个)。我还将添加一些我在教程之后创建的其他页面。
    • 您好,我明白您的意思了,由于缩进,没有检测到返回。但是在更正之后我仍然得到这个错误调用: The method is not allowed for the requested URL on log.html
    • 你重新加了GET吗?
    【解决方案2】:

    这个问题已经回答了好几次了,但我花了一段时间才明白我在寻找什么,具体到我的(略有不同的)问题,所以我只想在这里抛出通用答案:

    在您的代码中,您定义了一条路线,但没有返回任何内容。此代码返回一个模板:

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

    此代码仅在某些情况下返回模板:

    @app.route('/welcome')
    def welcome():
        if request.method == "POST":
            return render_template("welcome.html")
    

    因此,如果您真的不确定为什么会看到 ValueError: View function did not return a response 遍历您的代码,并确保您的所有路由定义 (def foo():) 实际上是 return 某些东西,并且某些东西没有嵌套在if 子句

    这个错误并没有提供很多关于它卡在哪里的线索,这很烦人,但我相信更有经验的 Pythoner 可以解释为什么会这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-14
      • 2014-04-01
      • 1970-01-01
      • 2018-05-16
      • 2015-09-26
      • 1970-01-01
      • 2017-08-24
      • 2014-09-21
      相关资源
      最近更新 更多