1

I'm working on a project that pulls a list of all the spirits listed in the Oregon State liquor pricing sheet and stores it in a table in my flask app. The goal is to be able to let moderators see the list and check a box next to each spirit to designate if it is whiskey or not.

It's my first time really using flask, but I've gone through a book about flask twice now to try and understand it, so this is me trying to build my own thing.

Right now, if I check a box and then hit submit all the records are switched to is_whiskey = False instead of each one being done individually.

Here's my model:

class Whiskies(db.Model):
    __tablename__ = 'whiskies'
    id = db.Column(db.Integer, primary_key=True)
    ols_name = db.Column(db.String(64), unique=True)
    display_name = db.Column(db.String(64), unique=True, index=True)
    display_distiller = db.Column(db.String(64), unique=False)
    is_whiskey = db.Column(db.Boolean, default=True)
    reviews = db.relationship('Review', backref='review', lazy='dynamic')

Here's my view:

@main.route('/update-whiskies', methods=['GET', 'POST'])
@login_required
@permission_required(Permission.MODERATE)
def update_whiskies():
    whiskies = Whiskies.query.all()
    is_whiskey_form = IsWhiskeyForm()
    submitpageform = SubmitPageForm()
    if submitpageform.validate_on_submit():
        for w in whiskies:
            w.is_whiskey = is_whiskey_form.is_whiskey.data
            db.session.update(w)
        db.session.commit()
        flash("You updated a bunch of whiskies")
        return redirect(url_for('.index'))
    return render_template('update_whiskies.html', whiskies=whiskies,
                           is_whiskey_form=is_whiskey_form,
                           submitpageform=submitpageform)

Here's my .html page:

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

{% block page_content %}
<div>
    {% for whiskey in whiskies %}
    <li class="whiskey">
        {% if whiskey.display_name %}
            {{ whiskey.display_name }}
        {% else %}
            {{ whiskey.ols_name }}
        {% endif %}
        {{ is_whiskey_form.is_whiskey.label }}
        {{ is_whiskey_form.is_whiskey(value=whiskey.is_whiskey) }}
    </li>
    {% endfor %}
    {{ wtf.quick_form(submitpageform) }}
</div>
{% endblock %}

Here are my forms:

class IsWhiskeyForm(Form):
    "This is intended for use in bulk editing whiskies"
    is_whiskey = BooleanField("Is Whiskey", default="checked")


class SubmitPageForm(FlaskForm):
    "This is intended for submitting a batch edit"
    submit = SubmitField('Update Whiskies')

This is what the page currently looks like. I want to be able to check a box and hit submit and then have it reflected in the db.

This is what the page currently looks like

Thanks a lot to whoever is up to helping me with this. I've been stuck about a week and I just need to get past this hump to finish up my module.

2 Answers 2

1

I had a similar task to solve: producing a list of database records (1 record per row) and let the user choose one record. So I put a radiobox at the end of each row. The problem was how to inform the Flask app which row was checked. I came up with the following solution:

  1. You must make the submit button of the form trigger a POST request

  2. You need in the data you want to show in the browser one field which is unique so you can use it to identify a row (I had data from an SQL database to display, so the primary key of a record was a good solution)

  3. Add a radiobutton in your html-template inside the {% for ... %} loop like this:

{% for data in dataset.items %}
    ... display the data of the row, plus:
    <div>
        <input type="radio" class="form-check-input cursor-pointer" 
               name="check_line" value="{{ data._id }}">
    </div>
{% endfor %}

in the POST request data there will appear a name-value pair produced by the radiobuttons which consists of the name you provided (here: "check_line") and the value you provided. The trick is that the value can be used to identify the row (is unique) because the Flask-rendering-magic was used.

By the way: when testing POST-requests I often write:

if request.method == 'POST':
    print(request.values)

in the route-functions where POST needs to be handled. This will display all the POST input parameters on the Flask-console.

0
0

Try this

if submitpageform.validate_on_submit():
    for w in whiskies:
        w.is_whiskey = is_whiskey_form.is_whiskey.data
    db.session.commit()

Remove db.session.update(w)

2
  • I updated after posting to have it use db.session.add(w) instead in order to update. It's a common way of doing it from what I can tell. Any reason I wouldn't want that line in there? Jan 3, 2020 at 15:28
  • No, it didn't work. I tried it and the same issue occurs. Jan 4, 2020 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.