Commit 2873a783 by Vitor Eloidio

Initial commit

parents
env/
*.db
\ No newline at end of file
from flask import Flask, render_template, request, redirect, url_for
from models import db, Animal
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///animais.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
with app.app_context():
db.create_all()
@app.route('/')
def index():
animais = Animal.query.all()
return render_template('index.html', animais=animais)
@app.route('/cadastrar', methods=['GET', 'POST'])
def cadastrar():
if request.method == 'POST':
nome = request.form['nome']
especie = request.form['especie']
idade = int(request.form['idade'])
peso = float(request.form['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'))
return render_template('cadastrar.html')
@app.route('/editar/<int:id>', methods=['GET', 'POST'])
def editar(id):
animal = Animal.query.get_or_404(id)
if request.method == 'POST':
animal.nome = request.form['nome']
animal.especie = request.form['especie']
animal.idade = int(request.form['idade'])
animal.peso = float(request.form['peso'])
db.session.commit()
return redirect(url_for('index'))
return render_template('editar.html', animal=animal)
@app.route('/excluir/<int:id>')
def excluir(id):
animal = Animal.query.get_or_404(id)
db.session.delete(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):
__tablename__ = 'animais'
id = db.Column(db.Integer, primary_key=True)
nome = db.Column(db.String(100), nullable=False)
especie = db.Column(db.String(100), nullable=False)
idade = db.Column(db.Integer, nullable=False)
peso = db.Column(db.Float, nullable=False)
def __repr__(self):
return f'<Animal {self.nome}>'
\ No newline at end of file
Flask
Flask-SQLAlchemy
\ No newline at end of file
body {
font-family: Arial, sans-serif;
margin: 30px;
background-color: #f5fff5;
}
h1 {
color: darkgreen;
}
table {
width: 100%;
margin-top: 20px;
border-collapse: collapse;
background-color: white;
}
th, td {
padding: 10px;
text-align: center;
}
a {
text-decoration: none;
margin: 5px;
}
input {
padding: 8px;
width: 250px;
}
button {
padding: 10px;
}
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Cadastrar Animal</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>➕ Cadastro de Animal</h1>
<form method="POST">
<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" required><br><br>
<input type="number" step="0.1" name="peso" placeholder="Peso" required><br><br>
<button type="submit">Salvar</button>
</form>
<br>
<a href="/">⬅ Voltar</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Editar Animal</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>✏️ Editar Animal</h1>
<form method="POST">
<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 }}" required><br><br>
<input type="number" step="0.1" name="peso" value="{{ animal.peso }}" required><br><br>
<button type="submit">Atualizar</button>
</form>
<br>
<a href="/">⬅ Voltar</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Animais da Fazenda</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>🐾 Lista de Animais da Fazenda</h1>
<a href="/cadastrar">➕ Cadastrar Animal</a>
<table border="1">
<tr>
<th>ID</th>
<th>Nome</th>
<th>Espécie</th>
<th>Idade</th>
<th>Peso</th>
<th>Ações</th>
</tr>
{% for animal in animais %}
<tr>
<td>{{ animal.id }}</td>
<td>{{ animal.nome }}</td>
<td>{{ animal.especie }}</td>
<td>{{ animal.idade }} ano</td>
<td>{{ animal.peso }} kg</td>
<td>
<a href="/editar/{{ animal.id }}">✏️ Editar</a>
<a href="/excluir/{{ animal.id }}">🗑️ Excluir</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