Commit 9a312db5 by Marina

cvsgdfd

parent ea3ff480
from flask import Flask, render_template, request, redirect
from flask import Flask, render_template, request, redirect, url_for
from models import db, Animal
app = Flask(__name__)
......@@ -9,11 +9,11 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
# Criar banco
# Criar banco de dados
with app.app_context():
db.create_all()
# LISTAR
# ROTA PRINCIPAL (LISTAR)
@app.route('/')
def index():
animais = Animal.query.all()
......@@ -23,16 +23,26 @@ def index():
@app.route('/cadastrar', methods=['GET', 'POST'])
def cadastrar():
if request.method == 'POST':
try:
nome = request.form['nome']
especie = request.form['especie']
idade = request.form['idade']
peso = request.form['peso']
idade = request.form.get('idade')
peso = request.form.get('peso')
novo = Animal(
nome=nome,
especie=especie,
idade=int(idade) if idade else None,
peso=float(peso) if peso else None
)
novo = Animal(nome=nome, especie=especie, idade=idade, peso=peso)
db.session.add(novo)
db.session.commit()
return redirect('/')
return redirect(url_for('index'))
except Exception as e:
return f"Erro ao cadastrar: {e}"
return render_template('cadastrar.html')
......@@ -42,23 +52,37 @@ def editar(id):
animal = Animal.query.get_or_404(id)
if request.method == 'POST':
try:
animal.nome = request.form['nome']
animal.especie = request.form['especie']
animal.idade = request.form['idade']
animal.peso = request.form['peso']
idade = request.form.get('idade')
peso = request.form.get('peso')
animal.idade = int(idade) if idade else None
animal.peso = float(peso) if peso else None
db.session.commit()
return redirect('/')
return redirect(url_for('index'))
except Exception as e:
return f"Erro ao editar: {e}"
return render_template('editar.html', animal=animal)
# EXCLUIR
@app.route('/excluir/<int:id>')
def excluir(id):
try:
animal = Animal.query.get_or_404(id)
db.session.delete(animal)
db.session.commit()
return redirect('/')
return redirect(url_for('index'))
except Exception as e:
return f"Erro ao excluir: {e}"
# EXECUTAR
if __name__ == '__main__':
app.run(debug=True)
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Sistema da Fazenda</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>🐄 Sistema da Fazenda</h1>
<nav>
<a href="/">Início</a> |
<a href="/cadastrar">Cadastrar Animal</a>
</nav>
<hr>
<!-- ESSENCIAL: sem isso nada aparece -->
{% block content %}{% endblock %}
</body>
</html>
\ No newline at end of file
......@@ -5,10 +5,10 @@
<h2>Cadastrar Animal</h2>
<form method="POST">
<input type="text" name="nome" placeholder="Nome" required><br>
<input type="text" name="especie" placeholder="Espécie" required><br>
<input type="number" name="idade" placeholder="Idade"><br>
<input type="number" step="0.1" name="peso" placeholder="Peso"><br>
<input type="text" name="nome" placeholder="Nome" required><br><br>
<input type="text" name="especie" placeholder="Espécie" required><br><br>
<input type="number" name="idade" placeholder="Idade"><br><br>
<input type="number" step="0.1" name="peso" placeholder="Peso"><br><br>
<button type="submit">Salvar</button>
</form>
......
......@@ -5,10 +5,10 @@
<h2>Editar Animal</h2>
<form method="POST">
<input type="text" name="nome" value="{{ animal.nome }}" required><br>
<input type="text" name="especie" value="{{ animal.especie }}" required><br>
<input type="number" name="idade" value="{{ animal.idade }}"><br>
<input type="number" step="0.1" name="peso" value="{{ animal.peso }}"><br>
<input type="text" name="nome" value="{{ animal.nome }}" required><br><br>
<input type="text" name="especie" value="{{ animal.especie }}" required><br><br>
<input type="number" name="idade" value="{{ animal.idade }}"><br><br>
<input type="number" step="0.1" name="peso" value="{{ animal.peso }}"><br><br>
<button type="submit">Atualizar</button>
</form>
......
......@@ -4,6 +4,7 @@
<h2>Lista de Animais</h2>
{% if animais %}
<table>
<tr>
<th>Nome</th>
......@@ -25,7 +26,10 @@
</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>🚜 Nenhum animal cadastrado ainda.</p>
{% endif %}
{% endblock %}
\ 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