'Flask + sqlite + html
I need to make that when I click register on the site, the data is added as a new line in the database. But when the button is clicked, the site is simply updated, how to fix it? I tried to rewrite many times, because of my little knowledge of languages, nothing really worked out.Here is the python, html and css code
from flask import Flask, render_template, request, render_template_string, url_for
app = Flask(__name__)
z = -1
conn = sqlite3.connect(r'C:\Users\Иван\Desktop\Новая папка (2)\database.db', check_same_thread=False)
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS passwords(
id TEXT PRIMARY KEY,
login TEXT,
email TEXT,
password TEXT);
""")
conn.commit()
def search_in_db(login,email,password):
data = (login,email,password)
cur.execute("INSERT INTO passwords VALUES (?,?,?)", data)
data_1 = cur.execute(f"SELECT * FROM passwords;").fetchall()
return data_1
@app.route('/reg', methods=['POST', 'GET'])
def reg():
return render_template("signup.html")
def loop():
print(cur.execute("SELECT * FROM passwords").fetchall())
if request.method:
site = list(request.form)
for i in site:
if i == 'reg':
a = request.form['log']
b = request.form['email']
c = request.form['pass']
db_table_val(a, b, c)
data = search_in_db(site)
return render_template('index.html', message=list(data))
else:
return 'NOPE'
def db_table_val(log,email,passw):
cur.execute('INSERT INTO passwords (log,email,passw) VALUES (?, ?, ?)',
(log,email,passw))
conn.commit()
@app.route('/')
def main():
return 'haha'
app.run()
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
}
input {
height: 45px;
width: 100%;
color: #3B5983;
font-size: 14px;
line-height: 16px;
border: 2px solid #E9F2FF;
border-radius: 5px;
padding-left: 25px;
}
input:focus {
outline: none;
border: 2px solid #C1D9FD;
}
input:valid {
border-color: greenyellow;
}
input:invalid {
border-color: orange;
}
input:not(:focus):invalid {
border-color: #C1D9FD;
}
main {
background: #F1F5FE;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.circle {
position: absolute;
z-index: 1;
width: 2534px;
height: 2534px;
border-radius: 50%;
background: #F7FAFF;
box-shadow: 0px 4px 70px 6px rgba(217, 229, 255, 0.25);
animation-name: fadeCircle;
animation-duration: .7s;
animation-timing-function: ease-in-out;
animation-delay: .3s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
}
@keyframes fadeCircle {
0% {
width: 2534px;
height: 2534px;
}
25% {
width: 2000px;
height: 2000px;
}
50% {
width: 1500px;
height: 1500px;
}
75% {
width: 1000px;
height: 1000px;
}
100% {
width: 534px;
height: 534px;
}
}
.register-form-container {
opacity: 0;
position: relative;
z-index: 2;
max-width: 415px;
width: 100%;
background: #FFFFFF;
box-shadow: 0px 6px 50px rgba(217, 229, 255, 0.7);
border-radius: 20px;
padding-left: 30px;
padding-right: 30px;
padding-top: 38px;
padding-bottom: 38px;
animation-name: fadeForm;
animation-duration: .7s;
animation-timing-function: ease-in-out;
animation-delay: 1.4s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
}
@keyframes fadeForm {
0% {
opacity: 0;
}
25% {
opacity: .25;
}
50% {
opacity: .5;
}
75% {
opacity: .75;
}
100% {
opacity: 1;
}
}
.form-title {
color: #30507D;
font-weight: 500;
font-size: 20px;
line-height: 23px;
margin-bottom: 38px;
}
.form-field {
margin-bottom: 13px;
}
.button {
width: 100%;
font-weight: bold;
font-size: 14px;
display:block;
height: 45px;
background: #247FFF;
border-radius: 5px;
color:#fff;
text-transform: uppercase;
text-align: center;
line-height: 45px;
cursor: pointer;
border: none;
}
.button:hover {
background-color: #0D6CF2;
}
a.button {
text-decoration: none;
}
.button-google {
color:#C6CFDC;
background: #F2F6FF;
}
.button-google:hover {
background: #E2E6F0;
color: #fff;
}
.divider {
font-weight: 500;
font-size: 12px;
line-height: 14px;
color: #405D87;
text-align: center;
padding-top: 25px;
padding-bottom: 25px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
<title>Форма регистрации</title>
</head>
<body>
<main>
<div class ='circle'></div>
<div class='register-form-container'>
<h1 class="form-title">
Регистрация
</h1>
<div class="form-fields">
<p>
<input class="form-title" type="text" name="log" placeholder="Login">
</p>
<p>
<input class="form-title" type="email" name="email" placeholder="Email">
</p>
<p>
<input class="form-title" type="password" name="pass" placeholder="Password">
</p>
</div>
<div class="form-buttons">
<form action="" method="post">
<p>
<input class ="button" type="submit" placeholder="Sign Up" name="reg">
</p>
</form>
<div class="divider">или</div>
<a class="button">Google</a>
</div>
</div>
</main>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|