Why can I not add to database?

Viewed 31

I am trying to add a subscriber to a new database, and I have a try and except block, and it keeps going to my except block.

here is my models.py file

from . import db
from sqlalchemy.sql import func
from datetime import datetime





class Subscribers(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(200), nullable=False)
    last_name = db.Column(db.String(200), nullable=False)
    email = db.Column(db.String(200), nullable=False, unique=True)
    date_created = db.Column(db.DateTime, default=func.now())
    

    def __repr__(self):
        return '<Name  %r>' % self.id

Here is part of my views.py file where I am applying it. (and my imports)

from flask import Flask, Blueprint, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import smtplib
import os
import smtplib
from .models import Subscribers
from . import db


@ views.route('/subscriber-thank-you', methods = ['GET', 'POST'])
def subscriber_thank_you():
    if request.method == 'POST':
        subscriber_first_name=request.form.get("subscriber_first_name")
        subscriber_last_name=request.form.get("subscriber_last_name")
        subscriber_email=request.form.get("subscriber_email")
        


        new_subscriber_message = "You have a new subscriber!"
        subscriber_message="You have been subscribed. Thank you."
        server=smtplib.SMTP("smtp.gmail.com", 587)
        server.starttls()
        server.login(email, pw)
        server.sendmail(email,
                        subscriber_email, subscriber_message)
        server.sendmail(email, email, new_subscriber_message + "\n" + subscriber_first_name + " " + subscriber_last_name + "\n" + subscriber_email)




        title = "Thank You For Subscribing"
        
        #add to database
        new_subscriber = Subscribers(first_name=subscriber_first_name, last_name=subscriber_last_name, email=subscriber_email)
       
        

        try:
            db.session.add(new_subscriber)
            db.session.commit()
            return redirect(url_for('views.subscriber_thank_you'))
        except:
            return "There was an error adding your subscription."
    else:
        return render_template('subscribe_contact.html', title=title)
    

when I remove the try block and (which has the the db.session.add and db.session.commit) it works fine and goes to the page, sends the emails. but when I add it, it comes with my message "There was an error adding your subscription."

Any thoughts? Thank you.

After Barmar's response, I fixed some the except part of the try and except block

try:
 db.session.add(new_subscriber)
 db.session.commit()
 return redirect(url_for('views.subscriber_thank_you'))
except NameError as e:
 return("There was an error adding your subscription. {e}")

after running it, I got this error message:

"sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: subscribers
[SQL: INSERT INTO subscribers (first_name, last_name, email, date_created) VALUES (?, ?, ?, CURRENT_TIMESTAMP)]
[parameters: ('dustin', 'oliver', 'dustinoliver12@gmail.com')]
(Background on this error at: https://sqlalche.me/e/14/e3q8)"
0 Answers
Related