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__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{os.path.join(basedir, 'database.db')}"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Pessoa(db.Model):
__tablename__ = 'pessoas'
id = db.Column(db.Integer, primary_key=True)
nome = db.Column(db.String(100), nullable=False)
sexo = db.Column(db.String(20), nullable=False)
data_nascimento = db.Column(db.String(20), nullable=False)
cidade = db.Column(db.String(100), nullable=False)
telefone = db.Column(db.String(20), nullable=True)
def __repr__(self):
return f"<Pessoa {self.nome}>"
@app.route('/')
def index():
pessoas = Pessoa.query.order_by(Pessoa.nome).all()
return render_template('index.html', pessoas=pessoas)
@app.route('/cadastro', methods=['GET', 'POST'])
def cadastro():
if request.method == 'POST':
nome = request.form.get('nome')
sexo = request.form.get('sexo')
data_nascimento = request.form.get('data_nascimento')
cidade = request.form.get('cidade')
telefone = request.form.get('telefone')
nova = Pessoa(
nome=nome,
sexo=sexo,
data_nascimento=data_nascimento,
cidade=cidade,
telefone=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
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Cadastro</title>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #43e97b, #38f9d7);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.card {
background: white;
padding: 30px;
border-radius: 12px;
width: 320px;
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}
h1 {
text-align: center;
margin-bottom: 20px;
}
label {
font-weight: bold;
margin-top: 10px;
display: block;
}
input, select {
width: 100%;
padding: 10px;
margin-top: 5px;
border-radius: 8px;
border: 1px solid #ccc;
}
button {
width: 100%;
margin-top: 15px;
padding: 12px;
border: none;
background: #43e97b;
color: white;
font-weight: bold;
border-radius: 8px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: #2ecc71;
}
.voltar {
display: block;
text-align: center;
margin-top: 10px;
text-decoration: none;
color: #555;
}
</style>
</head>
<body>
<div class="card">
<h1>Cadastro</h1>
<form method="POST">
<label>Nome</label>
<input type="text" name="nome" required>
<label>Sexo</label>
<select name="sexo">
<option>Masculino</option>
<option>Feminino</option>
</select>
<label>Nascimento</label>
<input type="date" name="data_nascimento" required>
<label>Cidade</label>
<input type="text" name="cidade" required>
<label>Telefone</label>
<input type="text" name="telefone">
<button type="submit">Salvar</button>
</form>
<a href="/" class="voltar">← Voltar</a>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Lista de Pessoas</title>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #4facfe, #00f2fe);
margin: 0;
padding: 20px;
}
.container {
background: white;
padding: 25px;
border-radius: 12px;
max-width: 900px;
margin: auto;
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}
h1 {
text-align: center;
color: #333;
}
.topo {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.btn {
background: #4facfe;
color: white;
padding: 10px 15px;
border-radius: 8px;
text-decoration: none;
font-weight: bold;
transition: 0.3s;
}
.btn:hover {
background: #007bff;
}
table {
width: 100%;
border-collapse: collapse;
overflow: hidden;
border-radius: 10px;
}
th {
background: #4facfe;
color: white;
padding: 12px;
}
td {
padding: 10px;
text-align: center;
}
tr:nth-child(even) {
background: #f2f2f2;
}
tr:hover {
background: #dfefff;
}
</style>
</head>
<body>
<div class="container">
<div class="topo">
<h1>Pessoas</h1>
<a href="/cadastro" class="btn">+ Novo</a>
</div>
<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