falsk 表单 无法执行,提示错误:ValueError: I/O operation on closed file.

发布于 2022-09-05 22:02:03 字数 3177 浏览 21 评论 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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人的歌 2022-09-12 22:02:03

首先,我承认一点,我没有使用过 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)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文