0
votes

Python code: (the main_

...   
from flask import Flask, render_template, url_for,request,flash,redirect         
from formzxc import Amount
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = '8ed085d7c0aefb62c65e9d2154c3f377'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///testing.db'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
class Category(db.Model):
     id = db.Column(db.Integer,primary_key = True)
     desc = db.Column(db.String(25),unique = True,nullable =False)
     amt = db.Column(db.Integer,nullable =False)
     def __repr__(self):
          return f"Category('{self.desc}'.'{self.amt}')"

@app.route('/',methods = ['GET','POST']) #need to research more on the methods
@app.route('/main',methods = ['GET','POST'])
def home():
    form = Amount() #this is the amount FlaskForm imported
    if form.validate_on_submit(): #validation is true
         flash(f'Done liao','success') #a message to put; put at homepage or prev page
         newpost = Category(desc = Amount.purpose.data,amt =Amount.amount.data ) ##
         db.session.add(newpost)
         db.session.commit()
         return redirect(url_for('home')) #function
    else:
         flash('Shit!','danger')
    return render_template(f'test.html',form  = form)

 if __name__ == '__main__':
    app.run(debug=True)

 ....

Python code(for formzxc) from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField, BooleanField,IntegerField from wtforms.validators import DataRequired, Length, Email, EqualTo

class Amount(FlaskForm):
    purpose = StringField('Type:',
                          validators = [DataRequired(),Length(max =25)])
    amount = IntegerField('Amount:',
                          validators = [DataRequired()])
    submit = SubmitField('Submit!')
1
Should you be using form.purpose.data instead of Amount.purpose.data in the call to Category?Iain Shelvington
^thks 4 the help tooDemonicAoi

1 Answers

0
votes

fix bellow error

newpost = Category(desc = Amount.purpose.data,amt =Amount.amount.data )

change to

newpost = Category(desc = form.purpose.data,amt =form.amount.data )

because ur are passing FlaskForm object to Amount class when creating it Amount class inherited the object form from it.So u have to use that object to get the data.