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 from models import db, Animal
app = Flask(__name__) app = Flask(__name__)
...@@ -9,11 +9,11 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False ...@@ -9,11 +9,11 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app) db.init_app(app)
# Criar banco # Criar banco de dados
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
# LISTAR # ROTA PRINCIPAL (LISTAR)
@app.route('/') @app.route('/')
def index(): def index():
animais = Animal.query.all() animais = Animal.query.all()
...@@ -23,16 +23,26 @@ def index(): ...@@ -23,16 +23,26 @@ def index():
@app.route('/cadastrar', methods=['GET', 'POST']) @app.route('/cadastrar', methods=['GET', 'POST'])
def cadastrar(): def cadastrar():
if request.method == 'POST': if request.method == 'POST':
nome = request.form['nome'] try:
especie = request.form['especie'] nome = request.form['nome']
idade = request.form['idade'] especie = request.form['especie']
peso = request.form['peso'] idade = request.form.get('idade')
peso = request.form.get('peso')
novo = Animal(nome=nome, especie=especie, idade=idade, peso=peso) novo = Animal(
db.session.add(novo) nome=nome,
db.session.commit() especie=especie,
idade=int(idade) if idade else None,
peso=float(peso) if peso else None
)
db.session.add(novo)
db.session.commit()
return redirect(url_for('index'))
return redirect('/') except Exception as e:
return f"Erro ao cadastrar: {e}"
return render_template('cadastrar.html') return render_template('cadastrar.html')
...@@ -42,23 +52,37 @@ def editar(id): ...@@ -42,23 +52,37 @@ def editar(id):
animal = Animal.query.get_or_404(id) animal = Animal.query.get_or_404(id)
if request.method == 'POST': if request.method == 'POST':
animal.nome = request.form['nome'] try:
animal.especie = request.form['especie'] animal.nome = request.form['nome']
animal.idade = request.form['idade'] animal.especie = request.form['especie']
animal.peso = request.form['peso']
db.session.commit() idade = request.form.get('idade')
return redirect('/') 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(url_for('index'))
except Exception as e:
return f"Erro ao editar: {e}"
return render_template('editar.html', animal=animal) return render_template('editar.html', animal=animal)
# EXCLUIR # EXCLUIR
@app.route('/excluir/<int:id>') @app.route('/excluir/<int:id>')
def excluir(id): def excluir(id):
animal = Animal.query.get_or_404(id) try:
db.session.delete(animal) animal = Animal.query.get_or_404(id)
db.session.commit() db.session.delete(animal)
return redirect('/') db.session.commit()
return redirect(url_for('index'))
except Exception as e:
return f"Erro ao excluir: {e}"
# EXECUTAR
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True) 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 @@ ...@@ -5,10 +5,10 @@
<h2>Cadastrar Animal</h2> <h2>Cadastrar Animal</h2>
<form method="POST"> <form method="POST">
<input type="text" name="nome" placeholder="Nome" required><br> <input type="text" name="nome" placeholder="Nome" required><br><br>
<input type="text" name="especie" placeholder="Espécie" required><br> <input type="text" name="especie" placeholder="Espécie" required><br><br>
<input type="number" name="idade" placeholder="Idade"><br> <input type="number" name="idade" placeholder="Idade"><br><br>
<input type="number" step="0.1" name="peso" placeholder="Peso"><br> <input type="number" step="0.1" name="peso" placeholder="Peso"><br><br>
<button type="submit">Salvar</button> <button type="submit">Salvar</button>
</form> </form>
......
...@@ -5,10 +5,10 @@ ...@@ -5,10 +5,10 @@
<h2>Editar Animal</h2> <h2>Editar Animal</h2>
<form method="POST"> <form method="POST">
<input type="text" name="nome" value="{{ animal.nome }}" required><br> <input type="text" name="nome" value="{{ animal.nome }}" required><br><br>
<input type="text" name="especie" value="{{ animal.especie }}" required><br> <input type="text" name="especie" value="{{ animal.especie }}" required><br><br>
<input type="number" name="idade" value="{{ animal.idade }}"><br> <input type="number" name="idade" value="{{ animal.idade }}"><br><br>
<input type="number" step="0.1" name="peso" value="{{ animal.peso }}"><br> <input type="number" step="0.1" name="peso" value="{{ animal.peso }}"><br><br>
<button type="submit">Atualizar</button> <button type="submit">Atualizar</button>
</form> </form>
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
<h2>Lista de Animais</h2> <h2>Lista de Animais</h2>
{% if animais %}
<table> <table>
<tr> <tr>
<th>Nome</th> <th>Nome</th>
...@@ -25,7 +26,10 @@ ...@@ -25,7 +26,10 @@
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
{% else %}
<p>🚜 Nenhum animal cadastrado ainda.</p>
{% endif %}
{% endblock %} {% 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