Commit db380165 by Jheni

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 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')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Modelo da Tabela com o novo campo 'telefone'
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)) # Campo adicionado
@app.route('/')
def index():
# Busca todos os registros para listar na tabela
pessoas = Pessoa.query.all()
return render_template('index.html', pessoas=pessoas)
@app.route('/cadastro', methods=['GET', 'POST'])
def cadastro():
if request.method == 'POST':
# Cria um novo objeto com os dados vindos do formulário HTML
nova = Pessoa(
nome=request.form['nome'],
sexo=request.form['sexo'],
data_nascimento=request.form['data_nascimento'],
cidade=request.form['cidade'],
telefone=request.form['telefone'] # Recebe o 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() # Cria o banco de dados se ele não existir
app.run(debug=True)
\ No newline at end of file
File added
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Cadastro</title>
</head>
<body>
<h1>Nova Pessoa</h1>
<form method="POST">
<label>Nome:</label><br>
<input type="text" name="nome" required><br><br>
<label>Sexo:</label><br>
<select name="sexo">
<option value="Masculino">Masculino</option>
<option value="Feminino">Feminino</option>
</select><br><br>
<label>Nascimento:</label><br>
<input type="date" name="data_nascimento" required><br><br>
<label>Cidade:</label><br>
<input type="text" name="cidade" required><br><br>
<label>Telefone:</label><br>
<input type="text" name="telefone" placeholder="(00) 00000-0000" required><br><br>
<button type="submit">Salvar Cadastro</button>
</form>
<br>
<a href="/">Voltar para a lista</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<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; width: 100%; text-align: left;">
<thead>
<tr>
<th>Nome</th>
<th>Sexo</th>
<th>Data de Nascimento</th>
<th>Cidade</th>
<th>Telefone</th>
</tr>
</thead>
<tbody>
{% 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 %}
</tbody>
</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