0

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>
0

1 Answer 1

1

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">
Sign up to request clarification or add additional context in comments.

2 Comments

I tried doing that as I have seen other suggestions online as well, but it didn't fix the problem
Thank you very much! The only problem is that I had to pass keywords as well in the Submission (Submission(email= email_content,subject= subject_content,message= message_content). It finally worked, thanks!

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.