每当我尝试运行我的代码时,我都会继续生成此错误,我查看了wtforms文档,但我找不到任何内容。这是我第一次使用wtforms和jinja2,所以我不知道是否错误地声明了什么。 这是我的app.py
from flask import Flask, render_template, redirect, url_for, flash, request
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from wtforms import StringField, PasswordField, BooleanField
from flask_wtf import FlaskForm
from wtforms.validators import InputRequired
app = Flask(__name__)
app.config['SECRET_KEY'] = 'Thisissupposedtobesecret!'
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////Users/app/app.db"
app.debug = True
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
relationship = db.Table('relationship',
db.Column('blog_id', db.Integer, db.ForeignKey('blogs.id')),
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')))
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(225), unique=True, nullable=False)
password = db.Column(db.String(225), nullable=False)
blogs = db.relationship('Blog', backref=db.backref('author'), lazy='dynamic')
def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password
def __repr__(self):
return '<User %r>' % self.username
class Blog(db.Model):
__tablename__ = 'blogs'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
blogname = db.Column(db.Integer, unique=True, nullable=False)
body = db.Column(db.String(600), nullable=False)
tags = db.relationship('Tags', secondary=relationship,
backref=db.backref('tagged'), lazy='dynamic')
def __init__(self, blogname, body):
self.blogname = blogname
self.body = body
def __repr__(self):
return '<Info %r>' % self.blogname
class Tags(db.Model):
__tablename__ = 'tags'
id = db.Column(db.Integer, primary_key=True)
tagname = db.Column(db.String(80), unique=True)
def __init__(self, tagname):
self.tagname = tagname
def __repr__(self):
return '<Tags %r>' % self.tagname
class LoginForm(FlaskForm):
username = StringField('username', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
remember = BooleanField('remember me')
class SignupForm(FlaskForm):
email = StringField('email', validators=[InputRequired()])
username = StringField('username', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
# this will be the home page
@app.route('/')
def index():
return render_template('dashboard.html')
@app.route('/signup', methods=['POST'])
def signup():
form = SignupForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
user = User(username=form.username.data, email=form.email.data, password=form.password.data)
db.session.add(user)
db.session.commit()
return render_template('dashboard.html', form=form)
这是我的html代码,使用jinja2,在这里我希望用户能够在线添加帐户,然后将该帐户提交到数据库
{% import "wtf.html" as wtf %}
{% block title %}
Sign up
{% endblock %}
{% block styles %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
{% endblock %}
{% block content %}
<div class="form">
<div id="signup">
<h1>Sign Up for Free</h1>
<form method="POST" action=“{{ url_for('signup')}}”>{{ csrf_input }}
<div class="top-row">
<div class="field-wrap">
{{wtf.form_field(form.username)}}
</div>
<div class="field-wrap">
{{wtf.form_field(form.email)}}
</div>
<div class="field-wrap">
{{wtf.form_field(form.password)}}
</div>
</div>
<input type="submit"/>
</form>
</div>
</div>
{% endblock %}
我的追溯错误是
UndefinedError: 'form' is undefined
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/amy.faulkner/app/app.py", line 82, in index
return render_template('dashboard.html')
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
context, ctx.app)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/flask/templating.py", line 116, in _render
rv = template.render(context)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/amy.faulkner/app/templates/dashboard.html", line 11, in top-level template code
{% block content %}
File "/Users/amy.faulkner/app/templates/dashboard.html", line 18, in block "content"
{{wtf.form_field(form.username)}}
File "/Users/amy.faulkner/app/venv/lib/python2.7/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
答案 0 :(得分:1)
你可以试试这个。 在utils.html中创建render_field
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
并在模板中导入此文件
{% from "utils.html" import render_field %}
<form method=post action="{{ url_for('signup')}}">
<div>
{{ render_field(form.username) }}
{{ render_field(form.email) }}
{{ render_field(form.password) }}
</div>
<p><input type=submit value=Register>
</form>
请参阅this文章了解更多详情
您可以在&#34; wtf.html&#34;中查看您的wtf宏方法。此错误即将发生,因为此处未定义任何表单