gpt4 book ai didi

python - Flask WTF 'StringField'对象没有属性 'translate'

转载 作者:行者123 更新时间:2023-12-03 15:49:12 26 4
gpt4 key购买 nike

我对Python比较陌生,并且一直关注Miguel Grinberg Flask Mega-Tutorial

我有一个非常简单的表格,当我尝试提交时,收到以下错误:

AttributeError: 'StringField' object has no attribute 'translate'



形式如下:
from flask.ext.wtf import Form
from wtforms import StringField
from wtforms.validators import DataRequired


class CreateSubjectForm(Form):
name = StringField('name', validators=[DataRequired()])

views.py:
@app.route('/create_subject', methods=['GET', 'POST'])
def create_subject():
form = CreateSubjectForm()
if form.validate_on_submit():
subject = Subject(name=form.name)
db.session.add(subject)
db.session.commit()
return render_template('create_subject.html',
title='Create Subject',
form=form)

我的模板如下:
{% extends "base.html" %}

{% block content %}
<h2>Create Subject</h2>
<form action="" method="post" name="create_subject">
{{ form.hidden_tag() }}
<div class="form-control-label">Name: </div>
<div class="form-control">{{ form.name(size=45) }}</div>
<input type="submit" value="Create">
</form>
{% endblock %}

我一直无法解决这个问题,但这是我所研究的。

由于PyCharm告诉我在 wtf行中的 flask.ext中找不到 from flask.ext.wtf import Form,这一事实引起了人们的关注。我正在使用Python 2.7。从研究看来,这是导入2.7的正确方法。我尝试将其更改为 from flask_wtf import Form,但PyCharm错误消失了,但是行为是相同的。

从追溯开始,我认为版本和软件包似乎出了点问题。我仔细检查了是否已用 pip.install安装了所有程序,看来我可以了-这是虚拟环境中的 pip.freeze:
Babel==2.2.0
blinker==1.4
coverage==4.0.3
decorator==4.0.9
Flask==0.10.1
Flask-Babel==0.9
Flask-Login==0.3.2
Flask-Mail==0.9.1
Flask-OpenID==1.2.5
Flask-SQLAlchemy==2.1
Flask-WhooshAlchemy==0.56
Flask-WTF==0.12
flipflop==1.0
guess-language==0.2
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
pbr==1.8.1
PyMySQL==0.7.2
python-openid==2.2.5
pytz==2016.3
six==1.10.0
speaklater==1.3
SQLAlchemy==1.0.12
sqlalchemy-migrate==0.10.0
sqlparse==0.1.19
Tempita==0.5.2
Werkzeug==0.11.5
Whoosh==2.7.3
WTForms==2.1

这是回溯:
> Traceback (most recent call last):   File
> "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1836, in __call__
> return self.wsgi_app(environ, start_response) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1820, in wsgi_app
> response = self.make_response(self.handle_exception(e)) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1403, in handle_exception
> reraise(exc_type, exc_value, tb) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1817, in wsgi_app
> response = self.full_dispatch_request() File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1477, in full_dispatch_request
> rv = self.handle_user_exception(e) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1381, in handle_user_exception
> reraise(exc_type, exc_value, tb) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1475, in full_dispatch_request
> rv = self.dispatch_request() File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\flask\app.py",
> line 1461, in dispatch_request
> return self.view_functions[rule.endpoint](**req.view_args) File "C:\Users\timni\onedrive\dev\studyplanner\app\views.py", line 24, in
> create_subject
> db.session.commit() File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\scoping.py",
> line 157, in do
> return getattr(self.registry(), name)(*args, **kwargs) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\session.py",
> line 801, in commit
> self.transaction.commit() File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\session.py",
> line 392, in commit
> self._prepare_impl() File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\session.py",
> line 372, in _prepare_impl
> self.session.flush() File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\session.py",
> line 2019, in flush
> self._flush(objects) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\session.py",
> line 2137, in _flush
> transaction.rollback(_capture_exception=True) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\util\langhelpers.py",
> line 60, in __exit__
> compat.reraise(exc_type, exc_value, exc_tb) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\session.py",
> line 2101, in _flush
> flush_context.execute() File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py",
> line 373, in execute
> rec.execute(self) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py",
> line 532, in execute
> uow File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\persistence.py",
> line 174, in save_obj
> mapper, table, insert) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\orm\persistence.py",
> line 800, in _emit_insert_statements
> execute(statement, params) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\engine\base.py",
> line 914, in execute
> return meth(self, multiparams, params) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\sql\elements.py",
> line 323, in _execute_on_connection
> return connection._execute_clauseelement(self, multiparams, params) File
> "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\engine\base.py",
> line 1010, in _execute_clauseelement
> compiled_sql, distilled_params File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\engine\base.py",
> line 1146, in _execute_context
> context) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\engine\base.py",
> line 1344, in _handle_dbapi_exception
> util.reraise(*exc_info) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\engine\base.py",
> line 1139, in _execute_context
> context) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\sqlalchemy\engine\default.py",
> line 450, in do_execute
> cursor.execute(statement, parameters) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\cursors.py",
> line 156, in execute
> query = self.mogrify(query, args) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\cursors.py",
> line 135, in mogrify
> query = query % self._escape_args(args, conn) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\cursors.py",
> line 115, in _escape_args
> return dict((key, conn.escape(val)) for (key, val) in args.items()) File
> "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\cursors.py",
> line 115, in <genexpr>
> return dict((key, conn.escape(val)) for (key, val) in args.items()) File
> "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\connections.py",
> line 781, in escape
> return escape_item(obj, self.charset, mapping=mapping) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\converters.py",
> line 26, in escape_item
> val = encoder(val, mapping) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\converters.py",
> line 109, in escape_unicode
> return u"'%s'" % _escape_unicode(value) File "C:\Users\timni\onedrive\dev\studyplanner\venv\lib\site-packages\pymysql\converters.py",
> line 72, in _escape_unicode
> return value.translate(_escape_table) AttributeError: 'StringField' object has no attribute 'translate'

所以在这一点上,我很沮丧。任何帮助将不胜感激。谢谢你。

最佳答案

发生错误是因为Subject实例是使用StringField实例构造的,但所需的是StringField的数据值。

所以代替

subject = Subject(name=form.name)  # form.name is a StringField


subject = Subject(name=form.name.data) 

线索是在异常消息 AttributeError: 'StringField' object has no attribute 'translate'中- translate是一个字符串方法,因此它建议将某个不是 string的内容传递给某个需要 string的内容。

关于python - Flask WTF 'StringField'对象没有属性 'translate',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36930346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com