flask-security中注册页面上的额外字段

时间:2014-02-07 02:13:23

标签: python flask flask-security

我正在尝试在Flask应用程序中创建一个注册页面。我正在使用Flask-Security进行用户管理。我已正确设置;标准注册页面确实正确呈现。但是,我的模型包含了许多额外的必填字段,因此我需要更新视图。

我的security_config文件如下所示:

from models import *
from flask_security.forms import ConfirmRegisterForm, Required

class ExtendedConfirmRegisterForm(ConfirmRegisterForm):
    first_name = CharField('Voornaam', [Required()])
    last_name = CharField('Achternaam', [Required()])        

# Setup Flask-Security
user_datastore = PeeweeUserDatastore(db, Student, Role, StudentRoleRel)
security = Security(app, user_datastore,
         confirm_register_form=ExtendedConfirmRegisterForm)

我的表格:

{% extends "base.html" %}
{% from "security/_macros.html" import render_field_with_errors, render_field %}
{% include "security/_messages.html" %}
{% block main%}
<h1>Registreer</h1>
<form action="{{ url_for_security('register') }}" method="POST" name="register_user_form">
  {{ register_user_form.hidden_tag() }}
  {{ render_field(register_user_form.first_name) }}
  {{ render_field(register_user_form.last_name) }}
  {{ render_field_with_errors(register_user_form.email) }}
  {{ render_field_with_errors(register_user_form.password) }}
  {% if register_user_form.password_confirm %}
    {{ render_field_with_errors(register_user_form.password_confirm) }}
  {% endif %}
  {{ render_field(register_user_form.submit) }}
</form>
{% endblock %}

当我尝试打开注册页面时,出现以下错误:

AttributeError: 'CharField' object has no attribute '__call__'

我真的不知道如何继续。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我自己发现了这个问题。

在我的安全配置文件中,我忘记导入TextField(在原版中,我使用的是CharField,但此类型不可用)

from models import *
from flask_security.forms import ConfirmRegisterForm, Required, TextField

class ExtendedConfirmRegisterForm(ConfirmRegisterForm):
    first_name = TextField('Voornaam', [Required()])
    last_name = TextField('Achternaam', [Required()])        

# Setup Flask-Security
user_datastore = PeeweeUserDatastore(db, Student, Role, StudentRoleRel)
security = Security(app, user_datastore,
         confirm_register_form=ExtendedConfirmRegisterForm)

答案 1 :(得分:0)

如果您收到类似jinja2.exceptions.UndefinedError: 'flask_security.forms.ConfirmRegisterForm object' has no attribute 'first_name'的信息

然后可能是由于SECURITY_CONFIRMABLE,您将其设置为true,这意味着flask将使用其他形式。更多详细信息:https://github.com/mattupstate/flask-security/issues/54