ValueError:视图函数没有在瓶中返回响应 [英] ValueError: View function did not return a response in flask

查看:133
本文介绍了ValueError:视图函数没有在瓶中返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



以下是route.py的代码

  from flask导入* 
from functools导入包装
app =烧瓶(__ name__)
app.secret_key =我的宝贝

@ 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)
返回重定向(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:$ b $如果request.form ['username']!='admin'或request.form ['password']!='admin':
error =凭证无效
else:
session ['logged_in'] = True
return redirect(url_for('hello'))
return render_template(' log.html',error = error)

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

代码为log.html

  { %扩展templates.html%} 
{%block content%}
< h1>登录< / h1>
{%如果错误%}
< p class = error> <强>错误:< / strong> {{error}}
{%endif%}
< form action =method =POST>
< dl>
< dt>用户名:
< dt>< input type =textname =usernamevalue ={{
request.form.username}}>
< dt>密码:
< dd>< input type =passwordname =passowrd>
< / dl>
< p>< input type =submitvalue =Login>
< / form>
{%endblock%}

templates.html的代码

 < html> 
< head>
< title>烧瓶教程(第1部分)< / title>
< / head>
< header>
< div class =navbar navbar-inverse>
< div class =navbar-inner>
< a class =brandhref =/> Real python(for the web!)< / a>
< ul>
< li>< a href =/ welcome>欢迎< / a>< / li>
< li>< a href =/ log>登入< / a>< / li>
< / ul>
< / div>
< / div>
< / header>
< body>
< div class =container>
{%block content%}
{%endblock%}
< / div>
< / body>
< / html>

home.html的代码

  {%extendstemplates.html%} 
{%block content%}
< div class =jumbo>
< h2>欢迎使用Flask< / h2>
< br />
< p>按一下< a href =/ welcome>这里< / a>去欢迎页面< / p>
< / div>
{%endblock%}

代码为hello.html

  {%extendstemplates.html%} 
{%block content%}
< h2>欢迎!您已登录。< / h2>
{%endblock%}

代表welcome.html

  {%extendstemplates.html%} 
{%block content%}
< h2>示例< / h2>
< p> Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。如果你想让自己的工作变得更加轻松,那么你就可以做到这一点。< / p>
< p> Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。如果你想让自己的工作变得更加轻松,那么你就可以做到这一点。< / p>
{%endblock%}

有人可以帮我解决问题吗?
在此先感谢

解决方案> log()方法如果您通过 GET 访问它,则不会返回响应。
你的问题在错误发生的时候是有点无用的,但是这是我可以推断出来的。

编辑:纵观视频,确实是 log 会导致您遇到麻烦的方法。
你已经不小心缩进了。

  @ app.route('/ log',methods = [' GET','POST'])
def log():
error = None $ b $如果request.method ==POST:$ b $如果request.form ['username' ]!='admin'或request.form ['password']!='admin':
error =证书无效
else:
session ['logged_in'] = True
return redirect(url_for('hello'))
return render_template('log.html',error = error)


So there are several of this questions already asked and the error for this means that the function has to return a value, or simply means it should return something.

I already have it in my routes.py file but its still not working.

Here is the code for 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)

Code for 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 %}

code for 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>

code for 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 %}  

code for hello.html

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

code for 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 %}  

Can someone help me where I am going wrong? Thanks in advance

解决方案

The log() method does not return a response if you're accessing it via GET. Your question is a bit uninformative where the error is happening, but this is what I can deduce.

Edit: Looking into the video, it is indeed the log method that is causing you trouble. You've accidentally indented too deep.

@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)

这篇关于ValueError:视图函数没有在瓶中返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆