2

I am new to Flask and I am trying to save form data from a Flask form to database using SQLAlchemy and I am not having any luck. I have tried several methods from research I found both here and outside of this forum.

When I take the simplistic route, the web form works and I can enter data but it will not populate the DB.

----Models----

class QIDMapping(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    qid_number = db.Column(db.Integer)
    br_field_name = db.Column(db.String(75))
    vendor_field = db.Column(db.String(75))

----Forms----

class QIDForm(Form):
    qidnumber = IntegerField('qidnumber', validators=[DataRequired()])
    brfieldname = StringField('brfieldname', validators=[DataRequired()])
    vendorfieldname = StringField('vendorfieldname')

----Views----

from flask import render_template, flash, redirect, session, url_for, 
request, g
from flask_wtf import form
from app import app, db
from .forms import QIDForm
from .models import User, QIDMapping
from flask.ext.sqlalchemy import SQLAlchemy


@app.route('/qidmapping', methods=['GET', 'POST'])
def qid_map_update():
    form = QIDForm()
    return render_template('qidmapping.html',
                            title='QID Mapping',
                            form=form)

----qidmapping.html----

{% block content %}

    <h1>Map QIDs to Vendor File</h1>
    <form action="" method="POST">
        {{form.hidden_tag()}}
        <p>
            Please enter the QID, BrassRing Field Name and Vendor Tag
            <br>
            <h2>QID Number {{ form.qidnumber(size=25) }}<br></h2>
            <h2>BR Field   {{ form.brfieldname(size=25) }}<br></h2>
            <h2>Vendor Field   {{ form.vendorfieldname(size=25) }}<br></h2>
        </p>
        <p><br>
        </p>
        <p><input type="submit" value="Save Fields">
        </p>
    </form>
{% endblock %}

I have also tried the method in this post Flask - WTForm - save form to db and when I do, I get a Method Not Allowed error and I'm not sure why.

----view for question 20837209 format----

@app.route('/qidmapping', methods=['GET', 'POST'])
def qid_map_update():
    form = QIDForm()
    if form.validate_on_submit():
        newform = (
            form.qidnumber.data,
            form.brfieldname.data,
            form.vendorfieldname.data
            )
        db.session.add(newform)
        db.session.commit()
        return redirect('/qidmapping')
    return render_template('qidmapping.html',
                            title='QID Mapping',
                            form=form)

Any help would be greatly appreciated!

1 Answer 1

3

Try replacing

newform = (
            form.qidnumber.data,
            form.brfieldname.data,
            form.vendorfieldname.data
            )
db.session.add(newform)

with

m = QIDMapping()
m.qid_number = form.qidnumber.data
m.br_field_name = form.brfieldname.data
m.vendor_field = form.vendorfieldname.data
db.session.add(m)

... and if that doesn't work. Do your standard POST troubleshooting:

1) Verify POST request

2) Ensure CSRF is working correctly.

3) Log validation errors / success

4) Check for DB exceptions

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Peter, I will give this a try!
No problem, glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.