Primeiro commit do CRUD

parents
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
# Caminho do banco de dados
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
db = SQLAlchemy(app)
# Modelo da Tabela
class Pessoa(db.Model):
id = db.Column(db.Integer, primary_key=True)
nome = db.Column(db.String(100))
sexo = db.Column(db.String(20))
data_nascimento = db.Column(db.String(20))
cidade = db.Column(db.String(100))
telefone = db.Column(db.String(100))
@app.route('/')
def index():
pessoas = Pessoa.query.all()
return render_template('index.html', pessoas=pessoas)
@app.route('/cadastro', methods=['GET', 'POST'])
def cadastro():
if request.method == 'POST':
nova = Pessoa(
nome=request.form['nome'],
sexo=request.form['sexo'],
data_nascimento=request.form['data_nascimento'],
cidade=request.form['cidade'],
telefone=request.form['telefone']
)
db.session.add(nova)
db.session.commit()
return redirect(url_for('index'))
return render_template('cadastro.html')
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
File added
blinker==1.9.0
click==8.3.1
colorama==0.4.6
Flask==3.1.3
Flask-SQLAlchemy==3.1.1
greenlet==3.3.2
itsdangerous==2.2.0
Jinja2==3.1.6
MarkupSafe==3.0.3
SQLAlchemy==2.0.48
typing_extensions==4.15.0
Werkzeug==3.1.7
body {
margin: 0;
font-family: 'Segoe UI', sans-serif;
background: linear-gradient(135deg, #f5e6e0, #fff);
overflow-x: hidden;
}
/* Barra lateral elegante */
.sidebar {
position: fixed;
left: 0;
top: 0;
width: 60px;
height: 100%;
background: #6d0f2b; /* vinho */
box-shadow: 3px 0 10px rgba(0,0,0,0.2);
}
/* Conteúdo */
.container {
margin-left: 80px;
padding: 20px;
}
/* Títulos */
h1 {
color: #6d0f2b;
}
/* Botão */
button {
background: #6d0f2b;
color: white;
border: none;
padding: 10px 15px;
border-radius: 8px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: #a61c44;
transform: scale(1.05);
}
/* Tabela */
table {
width: 100%;
border-collapse: collapse;
background: #fffaf8;
border-radius: 10px;
overflow: hidden;
}
th {
background: #6d0f2b;
color: white;
padding: 10px;
}
td {
padding: 10px;
text-align: center;
border-bottom: 1px solid #eee;
}
/* Inputs */
input, select {
padding: 8px;
border-radius: 6px;
border: 1px solid #ccc;
margin: 5px 0;
width: 100%;
}
/* Form */
form {
background: #fffaf8;
padding: 20px;
border-radius: 12px;
width: 300px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
/* 🌸 BOLHAS SUBINDO */
.bolhas span {
position: absolute;
bottom: -50px;
width: 20px;
height: 20px;
background: rgba(166, 28, 68, 0.4);
border-radius: 50%;
animation: subir 10s linear infinite;
}
.bolhas span:nth-child(1) { left: 10%; animation-duration: 8s; }
.bolhas span:nth-child(2) { left: 30%; animation-duration: 12s; }
.bolhas span:nth-child(3) { left: 50%; animation-duration: 10s; }
.bolhas span:nth-child(4) { left: 70%; animation-duration: 14s; }
.bolhas span:nth-child(5) { left: 90%; animation-duration: 9s; }
@keyframes subir {
0% {
transform: translateY(0);
opacity: 0.5;
}
100% {
transform: translateY(-800px);
opacity: 0;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Cadastro</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="sidebar"></div>
<div class="bolhas">
<span></span><span></span><span></span><span></span><span></span>
</div>
<div class="container">
<h1>Nova Pessoa</h1>
<form method="POST">
Nome:
<input type="text" name="nome" required>
Sexo:
<select name="sexo">
<option>Masculino</option>
<option>Feminino</option>
</select>
Nascimento:
<input type="date" name="data_nascimento" required>
Cidade:
<input type="text" name="cidade" required>
Telefone:
<input type="text" name="telefone" required>
<button type="submit">Salvar</button>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Lista de Pessoas</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="sidebar"></div>
<div class="bolhas">
<span></span><span></span><span></span><span></span><span></span>
</div>
<div class="container">
<h1>Pessoas Cadastradas</h1>
<a href="/cadastro"><button>Novo Cadastro</button></a>
<table>
<tr>
<th>Nome</th><th>Sexo</th><th>Data</th><th>Cidade</th><th>Telefone</th>
</tr>
{% for p in pessoas %}
<tr>
<td>{{ p.nome }}</td>
<td>{{ p.sexo }}</td>
<td>{{ p.data_nascimento }}</td>
<td>{{ p.cidade }}</td>
<td>{{ p.telefone }}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment