我有一个非常简单的表单,当我尝试提交时,我收到以下错误:
AttributeError: ‘StringFIEld’ object has no attribute ‘translate’
这是表格:
from flask.ext.wtf import Formfrom wtforms import StringFIEldfrom wtforms.valIDators import Datarequiredclass 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 >name: </div> <div >{{ form.name(size=45) }}</div> <input type="submit" value="Create"> </form>{% endblock %}
我一直无法解决这个问题,但这就是我所看到的.
PyCharm告诉我它无法在flask.ext.wtf导入Form的行中找到flask.ext中的wtf,这引起了一个担忧.我使用的是Python 2.7.从研究看来,这是导入2.7的正确方法.我尝试将其更改为来自flask_wtf导入Form并且PyCharm错误消失,但行为是相同的.
从追溯我认为看起来版本和软件包出了问题.我仔细检查我用pip.install安装了所有东西,看起来我做了 – 这是来自虚拟环境的pip.freeze:
Babel==2.2.0blinker==1.4coverage==4.0.3decorator==4.0.9Flask==0.10.1flask-babel==0.9Flask-Login==0.3.2Flask-Mail==0.9.1Flask-OpenID==1.2.5Flask-sqlAlchemy==2.1Flask-WhooshAlchemy==0.56Flask-WTF==0.12flipflop==1.0guess-language==0.2itsdangerous==0.24Jinja2==2.8MarkupSafe==0.23pbr==1.8.1PyMysqL==0.7.2python-openID==2.2.5pytz==2016.3six==1.10.0speaklater==1.3sqlAlchemy==1.0.12sqlalchemy-migrate==0.10.0sqlparse==0.1.19Tempita==0.5.2Werkzeug==0.11.5Whoosh==2.7.3WTForms==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,> 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_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,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",in <genexpr>> return dict((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'
所以在这一点上我很难过.任何帮助将非常感谢.谢谢.
解决方法 发生错误的原因是正在使用StringFIEld实例构造Subject实例,但需要的是StringFIEld数据的值.而不是
subject = Subject(name=form.name) # form.name is a StringFIEld
做
subject = Subject(name=form.name.data)
线索在异常消息AttributeError中:’StringFIEld’对象没有属性’translate’ – translate是一个字符串方法,所以它建议某些不是字符串的东西被传递给需要字符串的东西.
总结以上是内存溢出为你收集整理的python – Flask WTF’StringField’对象没有属性’translate’全部内容,希望文章能够帮你解决python – Flask WTF’StringField’对象没有属性’translate’所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)