Creating a form with a varying number of repeated subform in Flask/WTForms
Asked Answered
E

1

9

My model currently has three related objects (there are more, but only three are relevant to this problem). User, Network, and Email. What I want to be able to do is have a defined set of Networks, and to allow each User to have an Email address on each Network (these are slightly more complex, but I've cut them down to what I think is relevant).

class User(UserMixin, db.Model):
    """
    The User object.
    """
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    #    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    firstname = db.Column(db.String(64))
    lastname = db.Column(db.String(64), unique=False, index=True)
    email = db.relationship('Email', backref='user')

class Network(db.Model):
    __tablename__ = 'networks'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True)
    emails = db.relationship('Email', backref='network', lazy='dynamic')

class Email(db.Model):
    __tablename__ = 'emails'
    id = db.Column(db.Integer, primary_key=True)
    network_id = db.Column(db.Integer, db.ForeignKey('networks.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    address = db.Column(db.String(64))

My view:

@main.route('/edit-profile', methods=['GET', 'POST'])
@login_required
def edit_profile():
    form = EditProfileForm(obj=current_user)
    form.email.min_entries=Network.query.count()
    if form.validate_on_submit():
        form.populate_obj(current_user)
        db.session.add(current_user)
        db.session.commit()
        flash("Your profile has been updated.")
        return redirect(url_for('.user', username=current_user.username))
    return render_template('edit_profile.html', form=form)

And forms:

class EmailForm(Form):
    id = HiddenField('Id')
    address = StringField('Address', validators=[DataRequired(), Email()])
    network = QuerySelectField(query_factory=get_networks)


class EditProfileForm(Form):
    username = StringField('Username', validators=[Length(0, 64),
                                                   Regexp('[A-Za-z0-9_\.\-]'),
                                               DataRequired()])
    firstname = StringField('First name', validators=[Length(0, 64),
                                                      DataRequired()])
    lastname = StringField('Last name', validators=[Length(0, 64),
                                                    DataRequired()])
    email = ModelFieldList(FormField(EmailForm), model=Email)
    submit = SubmitField('Submit')

The outer form's HTML:

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Edit Profile{% endblock %}

{% block page_content %}
    <div class="page-header">
        <h1>Edit Your Profile</h1>
    </div>
    <div class="col-md-8">
        {{ wtf.quick_form(form) }}
    </div>
{% endblock %}

Here's what it looks like in both Chrome and Firefox:

Screenshot of horrid-looking form

So I'm obviously doing something wrong, since:

  1. The sub-form's widgets look nothing like those of the outer form, and
  2. The sub-form keeps being shown over the top of the outer form.

Where have I gone wrong with this? I tried not using wtf.quick_form() but couldn't get it to look right manually, either. To do that, I replaced the {{ wtf.quick_form() }} with this:

        <label>{{ form.username.label }}</label>
        {{ form.username }}
        <label>{{ form.firstname.label }}</label>
        {{ form.firstname }}
        <label>{{ form.lastname.label }}</label>
        {{ form.lastname }}
        <div data-toggle="fieldset" id="email-fieldset">
            {{ form.email.label }}
            <table class="ui table">
                <thead>
                <th>Network</th>
                <th>Address</th>
                <th>
                    {{ form_button(url_for('main.add_email'),

                            icon ('plus')) }}
                </th>
                </thead>
                <tbody>
                {% for e in form.email %}
                    <tr data-toggle="fieldset-entry">
                        <td>{{ e.network }}</td>
                        <td>{{ e.address }}</td>
                        <td>
                            {{ form_button(url_for('main.remove_email',
                                    id=loop.index), icon ('remove')) }}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {{ form.submit }}

When I render this, it appears as below in my browser:

Screenshot of bad form

This has the virtue of being consistent, but isn't the look I want to get using flask-bootstrap. I'm struggling to figure out which approach will get me where I want to go more easily.

SOLUTION

Changing the form html to this gave me the UI elements I was shooting for. The key was understanding that "class_" could be passed in and would be rendered in the output html as "class".

    <div class="form-group required"><label class="control-label">{{ form.username.label }}</label>
    {{ form.username(class_='form-control') }}</div>
    <div class="form-group required"><label class="control-label">{{ form.firstname.label }}</label>
    {{ form.firstname(class_='form-control') }}</div>
    <div class="form-group required"><label class="control-label">{{ form.lastname.label }}</label>
    {{ form.lastname(class_='form-control') }}</div>
    <div data-toggle="fieldset" id="email-fieldset" class="form-group">
        {{ form.email.label }}
        <table class="ui table">
            <thead>
            <th>Network</th>
            <th>Address</th>
            <th>
                {{ form_button(url_for('main.add_email'),

                        icon ('plus')) }}
            </th>
            </thead>
            <tbody>
            {% for e in form.email %}
                <tr data-toggle="fieldset-entry">
                    <td>{{ e.network(class_='form-control') }}</td>
                    <td>{{ e.address(class_='form-control') }}</td>
                    <td>
                        {{ form_button(url_for('main.remove_email',
                                id=loop.index), icon ('remove')) }}
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

Yielding this: Screenshot of correct form

Evy answered 5/6, 2015 at 13:39 Comment(5)
This could be a CSS problem. What does the output look like when you manually layout the form?Cusped
I added the screenshot above. I'm also working to try to get the solution proposed in #11310279 to work.Evy
What is this form_button thing in the template?Fincher
Can you post your main.add_email and main.remove_email functions? I'm curious how you did it.Helicoid
Flask-bootstrap is no longer supported, but one of the developers branched out a new version which renders properly, try it github.com/helloflask/bootstrap-flaskHiggle
E
4

The answer was to simply pass in "class_" to each field constructor within the .html form.

Evy answered 9/6, 2015 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.