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)
\ No newline at end of file
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 {
font-family: 'Segoe UI', sans-serif;
background: linear-gradient(135deg, #ffe6f0, #ffd6eb);
margin: 0;
padding: 0;
}
/* Container principal */
.container {
width: 80%;
margin: auto;
margin-top: 40px;
background: #fff0f6;
padding: 25px;
border-radius: 16px;
box-shadow: 0 8px 20px rgba(255, 105, 180, 0.2);
animation: fadeIn 0.5s ease-in-out;
}
/* Animação suave */
@keyframes fadeIn {
from {opacity: 0; transform: translateY(10px);}
to {opacity: 1; transform: translateY(0);}
}
/* Título */
h1 {
text-align: center;
color: #d63384;
margin-bottom: 20px;
}
/* Botões */
button {
background: linear-gradient(135deg, #ff99cc, #ff66b2);
color: white;
border: none;
padding: 10px 18px;
border-radius: 10px;
cursor: pointer;
font-weight: bold;
transition: 0.3s;
}
button:hover {
transform: scale(1.05);
background: linear-gradient(135deg, #ff66b2, #ff3385);
}
/* Tabela */
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
overflow: hidden;
border-radius: 10px;
}
th {
background: #ff99cc;
color: white;
padding: 10px;
}
td {
padding: 10px;
text-align: center;
}
/* Linhas alternadas */
tr:nth-child(even) {
background-color: #ffe6f0;
}
tr:hover {
background-color: #ffd6eb;
}
/* Inputs */
input, select {
padding: 10px;
margin: 8px 0;
border-radius: 8px;
border: 1px solid #ffb3d9;
outline: none;
transition: 0.3s;
}
input:focus, select:focus {
border-color: #ff66b2;
box-shadow: 0 0 5px #ff99cc;
}
/* Formulário */
form {
display: flex;
flex-direction: column;
}
/* Link botão */
a button {
margin-bottom: 10px;
}
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Cadastro</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>💖 Nova Pessoa</h1>
<form method="POST">
<label>Nome</label>
<input type="text" name="nome" placeholder="Digite o nome..." required>
<label>Sexo</label>
<select name="sexo">
<option>Masculino</option>
<option>Feminino</option>
</select>
<label>Data de Nascimento</label>
<input type="date" name="data_nascimento" required>
<label>Cidade</label>
<input type="text" name="cidade" placeholder="Sua cidade..." required>
<label>Telefone</label>
<input type="text" name="telefone" placeholder="(xx) xxxxx-xxxx" required>
<button type="submit">Salvar 💾</button>
</form>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Lista de Pessoas</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>🌸 Pessoas Cadastradas</h1>
<a href="/cadastro">
<button>+ Novo Cadastro</button>
</a>
<table>
<tr>
<th>Nome</th>
<th>Sexo</th>
<th>Nascimento</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>
\ No newline at end of file
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