Ben
Ben

Reputation: 1581

Extra fields on registration page in flask-security

I am trying to make a registration page in my Flask application. I am using Flask-Security for user management. I have set it up properly; the standard registration page did render correctly. However, my model consists of quite a few extra, required fields, so I needed to update the view.

My security_config file looks as follows:

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)

My form:

{% 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 %}

When I try to open the register page, I get the following error:

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

I don't really know how to proceed. How can I resolve this issue?

Upvotes: 1

Views: 2548

Answers (2)

olive_tree
olive_tree

Reputation: 1457

If you are getting something like: jinja2.exceptions.UndefinedError: 'flask_security.forms.ConfirmRegisterForm object' has no attribute 'first_name'

Then it is likely due to SECURITY_CONFIRMABLE, you set that to true which means flask will use a different form. More details: https://github.com/mattupstate/flask-security/issues/54

Upvotes: 1

Ben
Ben

Reputation: 1581

I found the problem myself.

In my security config file, I forgot to import TextField (in the original, I used CharField, but this type is unavailable)

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)

Upvotes: 1

Related Questions