简体   繁体   中英

Flask-WTForms can't find WTForms in my project directory

This is my first post on StackOverflow so Hello everyone.

I'm doing blog application to learn Python and Flask and I would like to launch it on Google App Engine. Unfortunately I have small problem with importing WTForms to the application. I'm currently using Flask 0.9, WTForms 1.0.1 and Flask-WTForms 0.8. I've added flaskext_wtf folder to root path of my project but I'm getting error from html5.py file.

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

It looks like it tries to find wtforms inside the extension path instead of my project path. How can I inform the html5.py file to look for the wtforms in the root?

Here are sources of my project - https://bitbucket.org/lucas_mendelowski/wblog/src

I think your Virtualenv does not have the Flask-WTF module. Write the following command in your command line under virtualenv

pip install Flask-WTF

or you may do this, but it is not recommended.

easy_install Flask-WTF

I think, I finally manage with this issue (but I'm not sure if it's the right way).

Change imports in these files:

1) flaskwtf/init_.py

From:

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

To:

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:

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

To:

import fields
import validators 
import widgets

I've also posted a solution on github - https://github.com/rduplain/flask-wtf/issues/46#issuecomment-7376577

You have incorrect import. You are probably doing:

from flask.ext.wtf import wtforms

There is no such module. Instead you should do:

from flask.ext.wtf import Form, TextField

If you want HTML5 widgets, just use the following. For example, you can import URLField as

from flask.ext.wtf.html5 import URLField

Your import is wrong. Also, make sure you install Flask-WTF which automatically installs WTForm.

As far as importing fields goes, according to the docs..

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()])

Please review the Flask-WTF quick start guide .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM