【问题标题】:Flask-WTForms can't find WTForms in my project directoryFlask-WTForms 在我的项目目录中找不到 WTForms
【发布时间】:2012-07-27 08:38:54
【问题描述】:

这是我在 StackOverflow 上的第一篇文章,大家好。

我正在做博客应用程序来学习 Python 和 Flask,我想在 Google App Engine 上启动它。不幸的是,我在将 WTForms 导入应用程序时遇到了小问题。我目前正在使用 Flask 0.9、WTForms 1.0.1 和 Flask-WTForms 0.8。我已将 flaskext_wtf 文件夹添加到我的项目的根路径,但我从 html5.py 文件中收到错误。

File "/Users/lucas/Workspace/blog/flask_wtf/html5.py", line 1, in <module>
from wtforms import TextField
File "/Users/lucas/Workspace/blog/flask/exthook.py", line 86, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named flask.ext.wtf.wtforms

看起来它试图在扩展路径而不是我的项目路径中查找 wtforms。如何通知 html5.py 文件在根目录中查找 wtforms?

这是我的项目的来源 - https://bitbucket.org/lucas_mendelowski/wblog/src

【问题讨论】:

  • 嗨 Lucas,你真的应该考虑在你的 Python 项目中使用 VirtualEnv。查看这本免费的在线书籍,了解有关良好 Flask 项目结构的完整指南:http://exploreflask.com

标签: python google-app-engine flask wtforms


【解决方案1】:

我认为您的 Virtualenv 没有 Flask-WTF 模块。 在 virtualenv 下的命令行中编写以下命令

pip install Flask-WTF

或者您可以这样做,但不建议这样做。

easy_install Flask-WTF

【讨论】:

    【解决方案2】:

    您的导入错误。另外,请确保您安装了 Flask-WTF,它会自动安装 WTForm。

    就导入字段而言,根据文档..

    From version 0.9.0, Flask-WTF will not import anything from wtforms 
    you need to import fields from wtforms.
    
    
        from flask_wtf import Form
        from wtforms import TextField
        from wtforms.validators import DataRequired
    
        class MyForm(Form):
            name = TextField('name', validators=[DataRequired()])
    

    请查看Flask-WTF quick start guide

    【讨论】:

      【解决方案3】:

      我想,我终于解决了这个问题(但我不确定这是否正确)。

      更改这些文件中的导入:

      1)flaskwtf/init_.py

      发件人:

      from flask.ext.wtf import html5
      from flask.ext.wtf.form import Form
      from flask.ext.wtf import recaptcha
      
      from flask.ext.wtf.recaptcha.fields import RecaptchaField
      from flask.ext.wtf.recaptcha.widgets import RecaptchaWidget
      from flask.ext.wtf.recaptcha.validators import Recaptcha
      

      收件人:

      import html5
      from form import Form
      import recaptcha
      
      from recaptcha.fields import RecaptchaField
      from recaptcha.widgets import RecaptchaWidget
      from recaptcha.validators import Recaptcha
      

      2)flaskwtf/recaptcha/init_.py:

      发件人:

      from flask.ext.wtf.recaptcha import fields
      from flask.ext.wtf.recaptcha import  validators 
      from flask.ext.wtf.recaptcha import  widgets
      

      收件人:

      import fields
      import validators 
      import widgets
      

      我还在 github 上发布了一个解决方案 - https://github.com/rduplain/flask-wtf/issues/46#issuecomment-7376577

      【讨论】:

        【解决方案4】:

        您的导入不正确。你可能正在做:

        from flask.ext.wtf import wtforms
        

        没有这样的模块。 相反,您应该这样做:

        from flask.ext.wtf import Form, TextField
        

        如果您想要 HTML5 小部件,只需使用以下内容。例如,您可以将 URLField 导入为

        from flask.ext.wtf.html5 import URLField
        

        【讨论】:

        • 感谢您的回复,但请在回答问题之前检查代码。在我的应用程序文件中,我得到了:from flask.ext.wtf import Form(我认为这是正确的)。但是在 Flask-WTForms 的 html5.py 文件中有:from wtforms import TextField 映射到:flask.ext.wtf.wtforms。
        • 我认为你应该只使用 Flask-WTF 而不是独立的 WTForms。
        猜你喜欢
        • 1970-01-01
        • 2017-05-16
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-10
        相关资源
        最近更新 更多