Commit 187f62f4 by Ayram

Ayram

parent d7b16b99
from models import db from flask import Flask, render_template, request, redirect
db.create_all() 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)
# Criar banco
with app.app_context():
db.create_all()
# Página principal (LISTAR)
@app.route('/')
def index():
animais = Animal.query.all()
return render_template('index.html', animais=animais)
# CADASTRO
@app.route('/cadastro', methods=['GET', 'POST'])
def cadastro():
if request.method == 'POST':
novo = Animal(
nome=request.form['nome'],
especie=request.form['especie'],
idade=request.form['idade'],
peso=request.form['peso']
)
db.session.add(novo)
db.session.commit()
return redirect('/')
return render_template('cadastro.html')
# EDITAR
@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 = request.form['idade']
animal.peso = request.form['peso']
db.session.commit()
return redirect('/')
return render_template('editar.html', animal=animal)
# EXCLUIR
@app.route('/excluir/<int:id>')
def excluir(id):
animal = Animal.query.get_or_404(id)
db.session.delete(animal)
db.session.commit()
return redirect('/')
# SOBRE
@app.route('/sobre')
def sobre():
return render_template('sobre.html')
if __name__ == '__main__':
app.run(debug=True)
\ No newline at end of file
...@@ -4,7 +4,7 @@ db = SQLAlchemy() ...@@ -4,7 +4,7 @@ db = SQLAlchemy()
class Animal(db.Model): class Animal(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
nome = db.Column(db.String(100)) nome = db.Column(db.String(100), nullable=False)
especie = db.Column(db.String(100)) especie = db.Column(db.String(100), nullable=False)
idade = db.Column(db.Integer) idade = db.Column(db.Integer, nullable=False)
peso = db.Column(db.Float) peso = db.Column(db.Float, nullable=False)
\ No newline at end of file \ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Cadastro</title>
</head>
<body>
<h1>Cadastrar Animal</h1>
<form method="POST"> <form method="POST">
<input type="text" name="nome" placeholder="Nome" required> Nome: <input type="text" name="nome" required><br>
<input type="text" name="especie" placeholder="Espécie" required> Espécie: <input type="text" name="especie" required><br>
<input type="number" name="idade" min="0" required> Idade: <input type="number" name="idade" required><br>
<input type="number" name="peso" step="0.1" min="0" required> Peso: <input type="number" step="0.1" name="peso" required><br>
<button type="submit">Cadastrar</button>
</form> <button type="submit">Salvar</button>
\ No newline at end of file </form>
<a href="/">Voltar</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Editar</title>
</head>
<body>
<h1>Editar Animal</h1>
<form method="POST">
Nome: <input type="text" name="nome" value="{{ animal.nome }}" required><br>
Espécie: <input type="text" name="especie" value="{{ animal.especie }}" required><br>
Idade: <input type="number" name="idade" value="{{ animal.idade }}" required><br>
Peso: <input type="number" step="0.1" name="peso" value="{{ animal.peso }}" required><br>
<button type="submit">Atualizar</button>
</form>
<a href="/">Voltar</a>
</body>
</html>
\ No newline at end of file
<ul> <!DOCTYPE html>
{% for animal in animais %} <html>
<li> <head>
{{ animal.nome }} | {{ animal.especie }} | <title>Fazenda</title>
{{ animal.idade }} anos | {{ animal.peso }} kg </head>
</li> <body>
{% endfor %}
</ul> <h1>🐄 Animais da Fazenda</h1>
\ No newline at end of file
<a href="/cadastro">Cadastrar</a> |
<a href="/sobre">Sobre</a>
<hr>
{% for animal in animais %}
<p>
<b>{{ animal.nome }}</b> ({{ animal.especie }}) |
Idade: {{ animal.idade }} |
Peso: {{ animal.peso }} kg
<a href="/editar/{{ animal.id }}">Editar</a>
<a href="/excluir/{{ animal.id }}">Excluir</a>
</p>
{% endfor %}
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Sobre</title>
</head>
<body>
<h1>Sobre o Projeto</h1>
<p>
Sistema CRUD para gerenciamento de animais de uma fazenda.
Permite cadastrar, listar, editar e excluir animais.
</p>
<a href="/">Voltar</a>
</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