Lone Creation
Lone Creation

Reputation: 69

How to get data from HTML using Flask and add it to SQLite?

I am trying to get user data using a form and adding it to the SQLite database with SQLAlchemy and Flask. I have tested the same code without SQLite and I was able to add it to a .csv file, however now that I am trying to add a database, I seem to run into issues, can anyone help me understand what is wrong with the code ?

my model:

class Submission(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(200), nullable = False)
    subject = db.Column(db.Text, nullable = False)
    message = db.Column(db.Text, nullable = False)

    def __repr__(self):
        return "<Submission %r>" % self.email

the view function

@app.route('/submit_form', methods=['POST', 'GET'])
def submit_form():
    if  request.method == "POST":
        email_content = request.form["email"]
        subject_content = request.form["subject"]
        message_content = request.form["message"]
        email = Submission(email=email_content)
        subject = Submission(email=subject_content)
        message = Submission(email=message_content)
        try: 
            db.session.add(email)
            db.session.add(subject)
            db.session.add(message)
            db.session.commit()
            return redirect("thankyou.html")
        except:
            return "did not save to database"
    else:
        return "something went wrong, try again"

and this is my form

<form action="submit_form" method="post" class="reveal-content">
  <div class="row">
    <div class="col-md-7">
      <div class="form-group">
        <input name="email" type="email" class="form-control" id="email" placeholder="Email">
      </div>
      <div class="form-group">
        <input name="subject" type="text" class="form-control" id="subject" placeholder="Subject">
      </div>
      <div class="form-group">
        <textarea name="message" class="form-control" rows="5" placeholder="Enter your message"></textarea>
      </div>
      <button type="submit" class="btn btn-default btn-lg">Send</button>
    </div>

Upvotes: 0

Views: 172

Answers (1)

cizario
cizario

Reputation: 4254

you'are doing things the wrong way in submit_form() function

@app.route('/submit_form', methods=['POST', 'GET'])
def submit_form():
    if  request.method == "POST":

        email_content = request.form["email"]
        subject_content = request.form["subject"]
        message_content = request.form["message"]

        # instantiate your Submission Model and populate it with the request parameters
        submission = Submission(email_content, subject_content, message_content)

        try: 
            db.session.add(submission)
            db.session.commit()
            return redirect("thankyou.html")
        except:
            return "did not save to database"
    else:
        return "something went wrong, try again"

you need to build the full route for submit_form view in the form action

<form action="{{ url_for('submit_form') }}" method="post" class="reveal-content">

Upvotes: 1

Related Questions