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__)
# Configuração
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Modelo
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(20))
# Página principal
@app.route('/')
def index():
pessoas = Pessoa.query.all()
return render_template('index.html', pessoas=pessoas)
# Cadastro
@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')
# Rodar
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: Arial, sans-serif;
background: linear-gradient(45deg, #feda75, #fa7e1e, #d62976, #962fbf, #4f5bd5);
margin: 0;
}
/* CONTAINER */
.container {
background: white;
width: 90%;
max-width: 800px;
margin: 40px auto;
padding: 20px;
border-radius: 20px;
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
}
/* TÍTULO */
h1 {
text-align: center;
color: #d62976;
}
/* TOPO */
.topo {
display: flex;
justify-content: space-between;
align-items: center;
}
/* BOTÕES */
button {
background: linear-gradient(45deg, #d62976, #962fbf);
color: white;
border: none;
padding: 10px 15px;
border-radius: 12px;
cursor: pointer;
font-weight: bold;
}
button:hover {
opacity: 0.8;
}
/* CARDS */
.card {
background: #fafafa;
margin-top: 15px;
padding: 15px;
border-radius: 15px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.nome {
font-size: 18px;
font-weight: bold;
color: #333;
}
.info {
color: #666;
margin-top: 5px;
}
/* FORMULÁRIO */
input, select {
width: 90%;
padding: 10px;
margin: 8px;
border-radius: 10px;
border: 1px solid #ccc;
}
/* LINKS */
a {
text-decoration: none;
color: #962fbf;
font-weight: bold;
}
\ 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>✨ Novo Perfil</h1>
<form method="POST">
<input type="text" name="nome" placeholder="Nome" required>
<select name="sexo">
<option>Masculino</option>
<option>Feminino</option>
</select>
<input type="date" name="data_nascimento" required>
<input type="text" name="cidade" placeholder="Cidade" required>
<input type="tel" name="telefone" placeholder="Telefone" required>
<button type="submit">Salvar</button>
</form>
<a href="/">← Voltar</a>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Pessoas</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<div class="topo">
<h1>📸 Pessoas</h1>
<a href="/cadastro">
<button>+ Novo</button>
</a>
</div>
{% for p in pessoas %}
<div class="card">
<div class="nome">{{ p.nome }}</div>
<div class="info">👤 {{ p.sexo }}</div>
<div class="info">🎂 {{ p.data_nascimento }}</div>
<div class="info">📍 {{ p.cidade }}</div>
<div class="info">📞 {{ p.telefone }}</div>
</div>
{% endfor %}
</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