Hi there
I created a charities.html with a form that allows me to add charities to a table and different buttons to
(1) submit the added charity (from the form)
(2) to view the results (currently the view is in the console only)
(3) create the database and table (obviously will be pressed only once to initiatte database and table). I created this button becasue this allows me to not have always reinvent the code when playing around with different columns.
(4) to check columns in table
(5) to delete the table
Button and their functions 3 to 5 seem to work. But (1) is just not working and I cannot explain why. There is the error message: Bad request and (4) does not show any result.
I have the following code in the main.py
from flask import Flask, redirect, request
import sqlite3
app = Flask(name)
app.secret_key = os.environ[‘sessionKey’]
@app.route(‘/’)
def index():
page = “”
f = open(“charities.html”,“r”)
page= f.read()
f.close()
return page
@app.route(‘/create_table’)
def creat_charity_list():
conn = sqlite3.connect(‘charities.db’)
c = conn.cursor()
c.execute(‘’‘DROP TABLE IF EXISTS charities’‘’)
c.execute(‘’‘CREATE TABLE IF NOT EXISTS charities
(id INTEGER PRIMARY KEY AUTOINCREMENT,
short_name_id,
long_name,
short_description)
;
‘’’)
conn.commit()
c.close()
conn.close()
print(“table is ready”)
return redirect(“/”)
@app.route(‘/add_charity’,methods=[“POST”])
def add_charity():
if request.method == “POST”:
form = request.form
long_name = form[“long_name”]
short_name_id = form[“short_name_id”]
short_description = form[“short_description”]
with sqlite3.connect("charities.db") as users:
cursor = users.cursor()
cursor.execute("INSERT INTO charities (long_name, short_name, short_description) VALUES (?,?,?)", (long_name,short_name_id, short_description))
users.commit()
print('charity added')
return redirect("/")
else:
return redirect(“/”)
@app.route(‘/read_table’)
def read_table():
conn = sqlite3.connect(‘charities.db’)
c = conn.cursor()
sqlite_select_query = “”“SELECT * from charities”“”
c.execute(sqlite_select_query)
records = c.fetchall()
print(“Total rows are: “, len(records))
print(“Printing each row”)
for row in records:
print(row[0])
print(row[1])
print(”\n”)
c.close()
conn.close()
return redirect(“/”)
@app.route(‘/delete_table’)
def delete_database():
conn = sqlite3.connect(‘charities.db’)
c = conn.cursor()
c.execute(‘’‘DROP TABLE IF EXISTS charities’‘’)
conn.commit()
c.close()
return redirect(“/”)
@app.route(‘/check_table’)
def check_table():
conn = sqlite3.connect(‘charities.db’)
c = conn.cursor()
data = c.execute(‘’‘SELECT * FROM charities’‘’)
for column in data.description:
print(column[0])
conn.commit()
conn.close()
return redirect(“/”)
app.run(host=‘0.0.0.0’, port=81)
Repl link: : https://replit.com/@bellyhills/productview#main.py
Any help would be highly appreciated!!!