Projeto Final: Sistema de Cadastro de Animais da Fazenda

parents
from flask import Flask, render_template, request, redirect, url_for
from models import db, Animal
import os
app = Flask(__name__)
# Configuração do Banco de Dados SQLite
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'fazenda.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
# Cria o banco de dados automaticamente se não existir
with app.app_context():
db.create_all()
@app.route('/')
def index():
animais = Animal.query.all()
return render_template('index.html', animais=animais)
@app.route('/adicionar', methods=['POST'])
def adicionar():
nome = request.form.get('nome')
especie = request.form.get('especie')
idade = request.form.get('idade')
peso = request.form.get('peso')
novo_animal = Animal(nome=nome, especie=especie, idade=idade, peso=peso)
db.session.add(novo_animal)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
\ No newline at end of file
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Animal(db.Model):
id = db.Column(db.Integer, primary_key=True)
nome = db.Column(db.String(100), nullable=False)
especie = db.Column(db.String(50), nullable=False)
idade = db.Column(db.Integer)
peso = db.Column(db.Float)
def __repr__(self):
return f'<Animal {self.nome}>'
\ No newline at end of file
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Fazenda Tech - Cadastro de Animais</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>🚜 Animais da Fazenda</h1>
<form action="/adicionar" method="POST" class="form-cadastro">
<input type="text" name="nome" placeholder="Nome do Animal" required>
<input type="text" name="especie" placeholder="Espécie (Ex: Vaca)" required>
<input type="number" name="idade" placeholder="Idade">
<input type="number" step="0.01" name="peso" placeholder="Peso (kg)">
<button type="submit">Cadastrar</button>
</form>
<hr>
<h2>Lista de Moradores</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Espécie</th>
<th>Idade</th>
<th>Peso</th>
</tr>
</thead>
<tbody>
{% for animal in animais %}
<tr>
<td>{{ animal.id }}</td>
<td>{{ animal.nome }}</td>
<td>{{ animal.especie }}</td>
<td>{{ animal.idade }} anos</td>
<td>{{ animal.peso }} kg</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
\ No newline at end of file
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f7f1;
color: #333;
display: flex;
justify-content: center;
padding: 20px;
}
.container {
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
width: 100%;
max-width: 800px;
}
h1 { color: #2e7d32; text-align: center; }
.form-cadastro {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-bottom: 20px;
}
input, button {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
grid-column: span 2;
background-color: #4caf50;
color: white;
border: none;
cursor: pointer;
font-weight: bold;
}
button:hover { background-color: #45a049; }
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border-bottom: 1px solid #eee;
padding: 12px;
text-align: left;
}
th { background-color: #e8f5e9; color: #2e7d32; }
\ 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