0
私はFlaskとWTFを初めて使用しており、ログインページの設定に問題があります。私は同様のエラーにつながるStackOverflowに関するいくつかの質問を見つけましたが、それらはテンプレートに渡されないフォームに起因しています。 login.htmlとその後、ここ未定義のエラー:フラスコ重量を使用している場合、 'wtfは未定義です'
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)
とされています:私はエラーを取得しておく
{% 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' 未定義である" ここで
はinput.pyときIログインページへのアクセスを試みます。ここではトレースバックは、次のとおりです。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
私はそれに気付かなかった、ありがとう! –