未定义错误:使用flask-wtf

时间:2017-09-20 11:13:59

标签: flask-wtforms flask-login

我是Flask和WTF的新手,我很难设置登录页面。我在StackOverflow上发现了一些导致类似错误的问题,但这些问题是由于表单没有传递给模板,我相信我已经做过了。

这是input.py:

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask import render_template, request, redirect, url_for, send_file
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import InputRequired, Email, Length

from models import *

app = Flask(__name__)

app.config['SECRET_KEY'] = 'thisisasecret'
Bootstrap(app)

app.config.from_pyfile('config.py')
db = SQLAlchemy(app)


class LoginForm(FlaskForm):
    username = StringField('username', validators=[InputRequired(), Length(min=4, max=15)])
    password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])
    remember = BooleanField('remember me')


@app.route('/')
def index():
    return render_template("index.html")


@app.route('/login')
def login():
    form = LoginForm()
    return render_template("login.html", form=form)

然后是login.html:

{% extends "layouts/layout1.html" %}
(% import "bootstrap/wtf.html" as wtf %}

{% block title %}
Login
{% endblock %}

{% block styles %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='signin.css')}}">
{% endblock %}

{% block content %}
    <div class="container">

      <form class="form-signin">
        <h2 class="form-signin-heading">Please sign in</h2>
          {{ form.hidden_tag() }}
          {{ wtf.form_field(form.username) }}
          {{ wtf.form_field(form.password) }}
          {{ wtf.form_field(form.remember) }}
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
      </form>

    </div> <!-- /container -->
{% endblock %}

当我尝试访问登录页面时,我不断收到错误“UndefinedError:'wtf'is undefined”。这是追溯:

Traceback (most recent call last):
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Bob\Desktop\input-master\input\input.py", line 39, in login
    return render_template("login.html", form=form)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\templating.py", line 134, in render_template
    context, ctx.app)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\flask\templating.py", line 116, in _render
    rv = template.render(context)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\jinja2\environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\jinja2\environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Bob\Anaconda3\lib\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Bob\Desktop\input-master\input\templates\login.html", line 1, in top-level template code
    {% extends "layouts/layout1.html" %}
  File "C:\Users\Bob\Desktop\input-master\input\templates\layouts\layout1.html", line 82, in top-level template code
    {% block content %}
  File "C:\Users\Bob\Desktop\input-master\input\templates\login.html", line 19, in block "content"
    {{ wtf.form_field(form.username) }}
  File "C:\Users\Bob\Anaconda3\lib\site-packages\jinja2\environment.py", line 408, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'wtf' is undefined

1 个答案:

答案 0 :(得分:2)

  

(%import&#34; bootstrap / wtf.html&#34; as wtf%}

看起来你的import语句开头有一个圆括号!