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 do banco
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))
# Lista
@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':
nome = request.form['nome']
sexo = request.form['sexo']
data_nascimento = request.form['data_nascimento']
cidade = request.form['cidade']
telefone = request.form['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')
# Excluir
@app.route('/deletar/<int:id>')
def deletar(id):
pessoa = Pessoa.query.get(id)
if pessoa:
db.session.delete(pessoa)
db.session.commit()
return redirect(url_for('index'))
# 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
<!DOCTYPE html>
<html>
<head>
<title>Cadastro</title>
</head>
<body>
<h1>Nova Pessoa</h1>
<form method="POST">
Nome: <input type="text" name="nome" required><br><br>
Sexo:
<select name="sexo">
<option>Masculino</option>
<option>Feminino</option>
</select><br><br>
Nascimento:
<input type="date" name="data_nascimento" required><br><br>
Cidade:
<input type="text" name="cidade" required><br><br>
Telefone:
<input type="tel" name="telefone" placeholder="(69) 99999-9999" required><br><br>
<button type="submit">Salvar</button>
</form>
<br>
<a href="/">
<button>Voltar</button>
</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Lista de Pessoas</title>
</head>
<body>
<h1>Pessoas Cadastradas</h1>
<a href="/cadastro">
<button>Novo Cadastro</button>
</a>
<table border="1" style="margin-top:10px">
<tr>
<th>Nome</th>
<th>Sexo</th>
<th>Data</th>
<th>Cidade</th>
<th>Telefone</th>
<th>Ações</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>
<td>
<a href="/deletar/{{ p.id }}">
<button onclick="return confirm('Tem certeza que deseja excluir?')">
Excluir
</button>
</a>
</td>
</tr>
{% endfor %}
</table>
</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