视图函数未返回有效响应 [英] The view function did not return a valid response

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

问题描述

我正在尝试遵循Miguel的出色教程( https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins ),复制并粘贴所需文本以尝试消除错误.我已经到达在模板中显示已登录的用户"部分,但是当我尝试运行该应用程序时,它可以正常运行,但浏览器显示错误,并且CMD会话中有诊断信息(我使用的是Windows).进一步提供.

I'm trying to follow Miguel's excellent tutorial to the letter (https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins), copying and pasting the required text to try and eliminate errors. I have reached the section "Showing The Logged In User in Templates" but when I try and run the app, it starts okay but the browser shows an error and there is diagnostic info within the CMD session (I'm using Windows). This is provided further down.

我是Python/Flask的新手,但是我猜一个被调用的例程没有返回值,但是在阅读完app.py代码后,我真的找不到我犯了什么错误.如果有人能指出正确的方向,我将继续本教程.

I am new to Python/Flask but I'm guessing that one of the called routines is not returning a value but, having read through the app.py code I really can't find what error I've made. If anyone can point me in the right direction I'd love to continue the tutorial.

routes.py文件具有登录代码:

The routes.py file has the login code:

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('index')
        return redirect(next_page)

是发生问题的..if form.validate_on_submit()..

我希望在127.0.0.1:5000上获得一个登录屏幕,但浏览器却显示:-

I expect to get a login screen on 127.0.0.1:5000 but instead the browser shows:-

内部服务器错误
服务器遇到内部错误,无法完成您的请求.服务器过载或应用程序出现错误.

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

被调用的URL是" http://127.0.0.1:5000/login?next =%2F "

CMD窗口显示以下错误消息:-

The CMD window shows the following error messages:-

[2019-09-07 18:13:47,941]应用程序错误:/login [GET]上的异常
追溯(最近一次通话):
wsgi_app中的文件"c:\ users \ tribl \ venv \ lib \ site-packages \ flask \ app.py",行2446
响应= self.full_dispatch_request()
1952行中的文件"c:\ users \ tribl \ venv \ lib \ site-packages \ flask \ app.py",格式为full_dispatch_request
返回self.finalize_request(rv)
在finalize_request中的文件"c:\ users \ tribl \ venv \ lib \ site-packages \ flask \ app.py",1967年
响应= self.make_response(rv)
在make_response
中,文件"c:\ users \ tribl \ venv \ lib \ site-packages \ flask \ app.py"第2097行 视图函数未返回有效响应."
TypeError:视图函数未返回有效响应.该函数返回None或在没有return语句的情况下结束.
127.0.0.1--[2019年9月7日18:13:47]"GET/login?next =%2F HTTP/1.1" 500-

[2019-09-07 18:13:47,941] ERROR in app: Exception on /login [GET]
Traceback (most recent call last):
 File "c:\users\tribl\venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
   response = self.full_dispatch_request()
 File "c:\users\tribl\venv\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
 File "c:\users\tribl\venv\lib\site-packages\flask\app.py", line 1967, in finalize_request
   response = self.make_response(rv)
 File "c:\users\tribl\venv\lib\site-packages\flask\app.py", line 2097, 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.
127.0.0.1 - - [07/Sep/2019 18:13:47] "GET /login?next=%2F HTTP/1.1" 500 -

推荐答案

该错误表明该函数未完成,因为它错过了返回指令.任何视图函数都必须返回某些内容.在您的情况下,您忘记了该行,该行用于指示模板文件在何处,尤其是用于呈现表单.您的代码应如下所示:

The error indicates that the function is not complete because it misses the return instruction. Any view function must return something. In your case you forgot this line which is used to indicate where is the template file and especially to render the form. Your code should look like this:

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('index')
        return redirect(next_page)
    return render_template('login.html', form=form)

标记该行:return render_template('login.html', form=form)

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

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