The view function did not return a valid response

妖精的绣舞 提交于 2020-05-15 09:53:39

问题


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.

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.

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)

It is the ..if form.validate_on_submit().. where the problem occurs

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.

The called URL is "http://127.0.0.1:5000/login?next=%2F"

The CMD window shows the following error messages:-

[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 -


回答1:


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)

remarks the line: return render_template('login.html', form=form)



来源:https://stackoverflow.com/questions/57835937/the-view-function-did-not-return-a-valid-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!