0

http://www.pythondoc.com/flas...
根据教程来做的,正在学习表单的写法。但是一直提示错误ValueError: I/O operation on closed file.

错误日志:

[2017-09-13 17:50:25,115] ERROR in app: Exception on /login [GET]
Traceback (most recent call last):
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/wwwroot/luejiao.com/app/views.py", line 34, in login
    form = LoginForm()
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/wtforms/form.py", line 212, in __call__
    return type.__call__(cls, *args, **kwargs)
  File "/home/wwwroot/luejiao.com/flask/lib/python3.6/site-packages/flask_wtf/form.py", line 157, in __init__
    ), stacklevel=3)
  File "/usr/local/lib/python3.6/warnings.py", line 101, in _showwarnmsg
    _showwarnmsg_impl(msg)
  File "/usr/local/lib/python3.6/warnings.py", line 30, in _showwarnmsg_impl
    file.write(text)
ValueError: I/O operation on closed file.

代码:
1.app/view.py

# -*- conding: utf-8 -*-
from flask import Flask, request, make_response, render_template, flash, redirect
from app import app
from .forms import LoginForm

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    return render_template('login.html', title='Sign In',form=form)

2.app/templates/login.html

<!-- extend from base layout -->
{% extends "base.html" %}

{% block content %}
  <h1>Sign In</h1>
  <form action="" method="post" name="login">
      {{ form.hidden_tag() }}
      <p>
          Please enter your OpenID:<br>
          {{ form.openid(size=80) }}<br>
      </p>
      <p>{{ form.remember_me }} Remember Me</p>
      <p><input type="submit" value="Sign In"></p>
  </form>
{% endblock %}

3.app/forms.py

from flask_wtf import Form
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired

class LoginForm(Form):
    openid = StringField('openid', validators=[DataRequired()])
    remember_me = BooleanField('remember_me', default=False)

之前在另外一台服务器弄过,没有这个错误。是不是系统有什么设置问题?

1个回答

1

已采纳

首先,我承认一点,我没有使用过 python3.6 来写 flask 程序。想必,你的 flask 框架也是最新的。建议你把forms 表单写成如下内容试试,当然,仅当试试,我没有测试过。


from flask_wtf import FlaskForm

from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired

class LoginForm(FlaskForm):
    openid = StringField('openid', validators=[DataRequired()])
    remember_me = BooleanField('remember_me', default=False)

修改的内容仅仅是将 from flask_wtf import Form 修改为from flask_wtf import FlaskForm,同时修改 class LoginForm(Form)class LoginForm(FlaskForm)

试试吧,成功与否,记得把结果告诉我。

撰写答案