realpython / discover-flask

Full Stack Web Development with Flask.
http://discoverflask.com
4.51k stars 803 forks source link

How to make a database for each html page in my web application? flask, python, html,sql #12

Open EldarU123 opened 4 years ago

EldarU123 commented 4 years ago

I'm currently building a flask web application using Microsoft Azure. The basic idea is just a blog for my school which I'm doing as a project. For this web application, I have two html pages. A home page where people just chat and a calendar page. yes not the best ideas but it's sufficient. What i'm wondering is how i select a database for each html page. Since it's like a forum blog type page, I already have the form and the script for that ready but when I enter something, it shows up for all the pages. I've made two databases, database.db for the home page and calendar.db for the calendar page. So far, everything i've been submitting into the forms is going into database.db and not calendar.db for the calendar.html page. So, i've made the database for the calendar page and made a table for it but can't seem to connect it to calendar.db.

Here is my code:

app.py


from flask_cors import CORS

from models import create_post, get_posts

app = Flask(__name__)

CORS(app)

u/app.route('/', methods=['GET','POST'])

def index():

if request.method == 'GET':

pass

if request.method == 'POST':

name = request.form.get('name')

post = request.form.get('post')

create_post(name, post)

posts = get_posts()

return render_template('index.html', posts=posts)

u/app.route('/calendar.html', methods=['GET','POST'])

def calendar():

if request.method == 'GET':

pass

if request.method == 'POST':

name = request.form.get('name')

post = request.form.get('post')

create_post(name, post)

posts = get_posts()

return render_template('calendar.html', posts=posts)

if __name__ == '__main__':

app.run(host="0.0.0.0", port=80)

and then models.py


import sqlite3 as sql

from os import path

ROOT = path.dirname(path.relpath((__file__)))

def create_post(name,content):

con = sql.connect(path.join(ROOT, 'database.db'))

cur = con.cursor()

cur.execute('insert into posts (name, content) values(?, ?)', (name, content))

con.commit()

con.close()

def get_posts():

con = sql.connect(path.join(ROOT, 'database.db'))

cur = con.cursor()

cur.execute('select * from posts')

posts = cur.fetchall()

return posts

and finally my html page for the calendar :


<!doctype html>

<center>

<head style="color:green; font-family: 'Times New Roman', Times, serif;font-size: 400px;">

<link rel="stylesheet" href="styles.css">

<font size = "+100">Important Events Coming Up</font>

<title>EcoNet</title>

</head>

<br>

<br>

<br>

<br>

<table>

<tr>

<th>

<a href="/">Parent Blog</a>

</th>

<th>

<a href="meeting.html">Teachers to Parents</a>

</th>

</tr>

</table>

<table >

<thead>

<tr>

<td >Mon</td>

<td>Tue</td>

<td>Wed</td>

<td>Thu</td>

<td>Fri</td>

<td>Sat</td>

<td>Sun</td>

</tr>

</thead>

<tbody>

<tr>

<td >29</td>

<td >30</td>

<td >31</td>

<td>1</td>

<td>2</td>

<td>3</td>

<td>4</td>

</tr>

<tr>

<td>5</td>

<td>6</td>

<td>7</td>

<td>8</td>

<td>9</td>

<td>10</td>

<td>11</td>

</tr>

<tr>

<td>12</td>

<td>13</td>

<td>14</td>

<td>15</td>

<td>16</td>

<td>17</td>

<td ></td>

</tr>

<tr>

<td>19</td>

<td>20</td>

<td>21</td>

<td>22</td>

<td>23</td>

<td>24</td>

<td>25</td>

</tr>

<tr>

<td>26</td>

<td>27</td>

<td>28</td>

<td>29</td>

<td>30</td>

<td>31</td>

<td >1</td>

</tr>

</tbody>

<h1 style="color:teal; font-family: 'Times New Roman', Times, serif;"> Type Down Below To Remind Anyone of Where and When the Gathering Will Happen</h1>

<body style = "background-color:lightgreen;">

<form action='/calendar.html' method='post'>

<input placeholder='Name' name='name'>

<input placeholder='Post Content' name='post'>

<input type='submit' value='Submit'>

</form>

{% for post in posts %}

<div>

{{ post[1] + ': ' + post[2] }}

</div>

{% endfor %}

</body>

</center>

</html>

my html is not the best.

It would be great if you guys could help me. I've made another models.py with different variables and connected them to calendar.html/calendar.db but that hasn't work. I've done a lot of things and followed the same steps i got database.db to work but I can't figure it out.

Thank you

RgnDunes commented 3 years ago

I don't understand the use of two seperate database, you can just create one database with multiple tables according to requirements.

But still if you want to use two database then you can refer these resources below : https://www.youtube.com/watch?v=SB5BfYYpXjE https://flask-appbuilder.readthedocs.io/en/latest/multipledbs.html