我正在通过LDAP登录管理器创建一个带有用户登录页面的flask网站。 LDAP连接没有问题,但是由于某种原因,Flask不允许我验证表单。我不断收到错误消息。我可以保持警惕。我希望你们能帮助我。
我的代码:
transmat_
我的HTML模板:
import os
from flask import Flask, render_template, redirect, url_for, make_response, request
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms.validators import InputRequired
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_user, login_required,logout_user, current_user
from flask_ldap3_login.forms import LDAPLoginForm
from flask_ldap3_login import LDAP3LoginManager, AuthenticationResponseStatus
import wtforms
from dbClass import User, Module, Category, SavingType, Status, ProjectOverview, Idea
#init app
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://///'path'/database.db'
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.init_app(app)
login_manager.login_view = 'login'
Bootstrap(app)
#Ldap connection
class Ldap(LoginManager):
# Setup LDAP Configuration Variables. Change these to your own settings.
# All configuration directives can be found in the documentation.
app.config['LDAP_HOST'] = 'host' # Hostname of your LDAP Server
app.config['LDAP_BASE_DN'] = 'dc=,dc=' # Base DN of your directory
app.config['LDAP_USER_DN'] = 'ou=users' # Users DN to be prepended to the Base DN
app.config['LDAP_GROUP_DN'] = 'ou=groups' # Groups DN to be prepended to the Base DN
app.config['LDAP_USER_RDN_ATTR'] = 'cn' # The RDN attribute for your user schema on LDAP
app.config['LDAP_USER_LOGIN_ATTR'] = 'sAMAccountName' # The Attribute you want users to authenticate to LDAP with.
app.config['LDAP_BIND_USER_DN'] = '????' # The Username to bind to LDAP with
app.config['LDAP_BIND_USER_PASSWORD'] = '?????' # The Password to bind to LDAP with
ldap_manager = LDAP3LoginManager(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class LDAPLoginForm(FlaskForm):
username = wtforms.StringField('Username', validators=[InputRequired()])
password = wtforms.PasswordField('Password', validators=[InputRequired()])
submit = wtforms.SubmitField('Submit')
remember_me = wtforms.BooleanField('Remember Me', default=True)
def validate_ldap(self):
ldap_mgr = app.ldap3_login_manager
username = self.username.data
password = self.password.data
result = ldap_mgr.authenticate(username, password)
if result.status == AuthenticationResponseStatus.success:
self.user = ldap_mgr._save_user(
result.user_dn,
result.user_id,
result.user_info,
result.user_groups
)
return True
else:
self.user = None
self.username.errors.append('Invalid Username/Password.')
self.password.errors.append('Invalid Username/Password.')
return False
@app.route('/')
def index():
return redirect('login', code=302)
@app.route('/login', methods=['GET', 'POST'])
def login():
frm = LDAPLoginForm()
if frm. validate_on_submit():
login_user(frm.user, remember=True)
return render_template('base.html',form=frm)
if not current_user or current_user.is_anonymous:
return render_template('login.html')
我得到的错误是“ jinja2.exceptions.UndefinedError:'form'未定义” 我尽了一切力所能及的
编辑:错误日志:
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}
Opportunity 2.0
{% endblock %}
{% block styles %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('.static', filename='css/signin.css') }}">
{% endblock %}
{% block content %}
<html style="background-image:url("static/img/adult-audience-band-553586.jpg");background-color:rgb(80,90,102);">
<div class="container">
<div class="d-inline-block registration-form" style="margin:9px;padding:25px;"><img src="static/img/BARCO_rgb_primarylogo_white.png" style="width:150px;margin:14px;padding:-13px;">
<form method="post" , action="/login" style="margin:12px;width:586px;">
<h2 class="form-signin-heading">Login</h2>
{{ frm.hidden_tag() }}
<p>
{{ frrm.username.label }}<br>
{{ frm.username(size=32) }}
</p>
<p>
{{ frm.password.label }}<br>
{{ frm.password(size=32) }}
</p>
<p>{{ frm.remember_me() }} {{ frm.remember_me.label }}</p>
<p>{{ frm.submit() }}</p>
<div class="form-group">
<button class="btn btn-primary btn-block create-account" type="submit"
style="background-color:rgb(240,0,0);width:174px;font- size:16px;font-family:Roboto, sans-serif;">
Login
</button>
</div>
</form>
</div> <!-- /container -->
{% endblock %}
提前
答案 0 :(得分:0)
您告诉Jinja它应访问的变量称为form
。即使您要传递的变量称为frm
,由于这一行,Jinja也会将其视为form
。
return render_template('base.html',form=frm)
因此您的模板需要看起来像这样
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}
Opportunity 2.0
{% endblock %}
{% block styles %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('.static', filename='css/signin.css') }}">
{% endblock %}
{% block content %}
<html style="background-image:url("static/img/adult-audience-band-553586.jpg");background-color:rgb(80,90,102);">
<div class="container">
<div class="d-inline-block registration-form" style="margin:9px;padding:25px;"><img src="static/img/BARCO_rgb_primarylogo_white.png" style="width:150px;margin:14px;padding:-13px;">
<form method="post" , action="/login" style="margin:12px;width:586px;">
<h2 class="form-signin-heading">Login</h2>
{{ form.hidden_tag() }}
<p>
{{ form.username.label }}<br>
{{ form.username(size=32) }}
</p>
<p>
{{ form.password.label }}<br>
{{ form.password(size=32) }}
</p>
<p>{{ form.remember_me() }} {{ frm.remember_me.label }}</p>
<p>{{ form.submit() }}</p>
<div class="form-group">
<button class="btn btn-primary btn-block create-account" type="submit"
style="background-color:rgb(240,0,0);width:174px;font- size:16px;font-family:Roboto, sans-serif;">
Login
</button>
</div>
</form>
</div> <!-- /container -->
{% endblock %}
如果仍然不起作用,请尝试将form变量传递给登录呈现模板。我不确定它们是否通过模板向下传递
if not current_user or current_user.is_anonymous:
return render_template('login.html', form=frm)