app.py corrigido (V0.1)

parent c382d123
......@@ -13,12 +13,7 @@ db = SQLAlchemy(app)
# Modelo da Tabela
class Receita(db.Model):
id = db.Column(db.Integer, primary_key=True)
nome = db.Column(db.String(100))
porcoes = db.Column(db.Integer)
tempo = db.Column(db.Integer)
descricao = db.Column(db.String(100))
ingredientes = db.Column(db.String(500))
preparo = db.Column(db.String(700))
nome = db.Column(db.String(100), nullable=False)
@app.route('/')
def index():
......@@ -29,19 +24,23 @@ def index():
def cadastro():
if request.method == 'POST':
nova = Receita(
nome=request.form['nome'],
porcoes=request.form['porcoes'],
tempo=request.form['tempo'],
descricao=request.form['descricao'],
ingredientes=request.form['ingredientes'],
preparo=request.form['preparo']
nome=request.form['nome']
)
db.session.add(nova)
db.session.commit()
return redirect(url_for('index'))
return render_template('cadastro.html')
@app.route('/editar/<int:id>', methods=['GET', 'POST'])
def editar(id):
receita = Receita.query.get_or_404(id)
if request.method == 'POST':
receita.nome = request.form['nome']
db.session.commit()
return redirect(url_for('index'))
return render_template('editar.html', receita=receita)
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
app.run(debug=True)
\ No newline at end of file
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Lexend:wght@100..900&display=swap" rel="stylesheet">
<title>Dishly</title>
<title>Cadastro</title>
</head>
<body>
<h1>Cadastro com Imagem</h1>
<div class="actions">
<a href="/" class="btn btn-primary">Voltar</a>
</div>
<form id="formCadastro" action="{{ url_for('cadastro') }}" method="post">
<input type="text" id="nome" name="nome" placeholder="Digite o nome" required><br>
<input type="text" id="porcoes" name="porcoes" placeholder="Porções" required><br>
<input type="text" id="tempo" name="tempo" placeholder="Tempo de preparo" required><br>
<textarea id="descricao" name="descricao" placeholder="Descrição da receita" required></textarea><br>
<input type="text" id="ad_ingrediente" placeholder="Ingrediente">
<button type="button" onclick="adicionar()">Adicionar</button>
<br><br>
<textarea id="ingredientes" name="ingredientes" rows="10" cols="30" readonly required></textarea><br>
<script>
function adicionar() {
let texto = document.getElementById("ad_ingrediente").value;
let textarea = document.getElementById("ingredientes");
if (!texto) return;
textarea.value += "- " + texto + "\n";
document.getElementById("ad_ingrediente").value = "";
}
</script>
<textarea id="preparo" name="preparo" placeholder="Modo de preparo" required></textarea><br>
<input type="submit" value="Cadastrar" class="btn btn-primary"><br>
<button type="reset">Apagar Dados</button>
</form>
<div class="cards" id="lista"></div>
<script>
const form = document.getElementById("formCadastro");
const lista = document.getElementById("lista");
form.addEventListener("submit", function (e) {
e.preventDefault();
const nome = document.getElementById("nome").value;
const arquivo = document.getElementById("imagem").files[0];
if (!arquivo) return;
const leitor = new FileReader();
leitor.onload = function (evento) {
const card = document.createElement("div");
card.classList.add("card");
card.innerHTML = `
<img src="${evento.target.result}">
<h3>${nome}</h3>
`;
lista.appendChild(card);
form.reset();
};
leitor.readAsDataURL(arquivo);
});
</script>
<div>
<form action="/cadastro" method="POST">
<label for="nome">Nome da Receita</label>
<input id="nome" name="nome" type="text" required>
<button type="reset">Limpar</button>
<button type="submit">Salvar</button>
</form>
</div>
</body>
</html>
\ No newline at end of file
......@@ -3,9 +3,16 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<title>Editar Receita</title>
</head>
<body>
<div>
<form action="/editar/{{ receita.id }}" method="POST">
<label for="nome">Nome da Receita</label>
<input id="nome" name="nome" type="text" value="{{ receita.nome }}" required>
<button type="reset">Limpar</button>
<button type="submit">Salvar</button>
</form>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="PT-br">
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Lexend:wght@100..900&display=swap" rel="stylesheet">
<title>Dishly</title>
</head>
<body>
<div class="container">
<header>
<h1>Receitas</h1>
<p class="subtitle">Gerenciamento de receitas</p>
</header>
<div class="actions">
<a href="/cadastro" class="btn btn-primary">➕ Nova Receita</a>
</div>
{% if receitas %}
<div class="table-responsive">
<table class="receitas-table">
<thead>
<tr>
<th>Nome</th>
<th>Porções</th>
<th>Tempo</th>
<th>Descrição</th>
<th>Ingredientes</th>
<th>Preparo</th>
</tr>
</thead>
<tbody>
{% for a in receitas %}
<tr>
<td><strong>{{ a.nome }}</strong></td>
<td>{{ a.porcoes }} porções</td>
<td>{{ a.tempo }} minutos</td>
<td>{{ a.descricao }}</td>
<td>{{ a.ingredientes }}</td>
<td>{{ a.preparo }}</td>
<td class="actions-column">
<a href="/editar/{{ a.id }}" class="btn btn-edit">✏️ Editar</a>
<a href="/delete/{{ a.id }}" class="btn btn-delete" onclick="return confirm('Tem certeza que deseja deletar?')">🗑️ Deletar</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<div class="empty-state">
<p>Nenhuma receita cadastrada ainda.</p>
<a href="/cadastro" class="btn btn-primary">Cadastre a primeira receita</a>
</div>
{% endif %}
</div>
<a href="/cadastro">Cadastrar Receita</a>
<h1>Lista de Receitas</h1>
<ul>
{% for receita in receitas %}
<li>
{{ receita.nome }}
<a href="/editar/{{ receita.id }}">Editar</a>
</li>
{% endfor %}
</ul>
</body>
</html>
</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