atualização

This commit is contained in:
2025-03-16 22:57:23 -03:00
parent d42352cda0
commit 92e0128425
7 changed files with 141 additions and 10 deletions

Binary file not shown.

61
app.py
View File

@@ -1,10 +1,63 @@
from flask import Flask, render_template from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app =Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
@app.route('/') class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Task %r>' % self.id
@app.route('/', methods=['POST', 'GET'])
def index(): def index():
return render_template('index.html') if request.method == 'POST':
task_content = request.form['content']
#task_content é o conteudo do form
new_task = Todo(content=task_content)
try:
db.session.add(new_task)
db.session.commit()
return redirect('/')
except:
return 'Error adding task'
else:
tasks = Todo.query.order_by(Todo.date_created).all()
#ordena e mostra todo database criado
return render_template('index.html', tasks=tasks)
@app.route('/delete/<int:id>')
def delete(id):
task_delete = Todo.query.get_or_404(id)
try:
db.session.delete(task_delete)
db.session.commit()
return redirect('/')
except:
return'error in delete'
@app.route('/update/<int:id>', methods=['GET', 'POST'])
def update(id):
task = Todo.query.get_or_404(id)
if request.method == 'POST':
task.content = request.form['content']
try:
db.session.commit()
return redirect('/')
except:
return'update error'
else:
return render_template('update.html', task=task)
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) app.run(debug=True)

View File

@@ -1,4 +1,36 @@
body { body, html {
margin: 0; margin: 0;
font-family: sans-serif; font-family: sans-serif;
background-color: lightblue;
}
.content {
margin: 0 auto;
width: 400px;
}
table, td, th {
border: 1px solid #aaa;
}
table {
border-collapse: collapse;
width: 100%;
}
th {
height: 30px;
}
td {
text-align: center;
padding: 5px;
}
.form {
margin-top: 20px;
}
#content {
width: 70%;
} }

View File

@@ -3,7 +3,8 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="" <meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
{% block head %}{% endblock %} {% block head %}{% endblock %}
</head> </head>
<body> <body>

View File

@@ -1,9 +1,36 @@
{% extends 'base.html'%} {% extends 'base.html' %}
{% block head %} {% block head %}
<title>Fazer coisas</title>
{% endblock %} {% endblock %}
{% block body %} {% block body %}
<h1>Template</h1> <div class="content">
<h1 style="text-align: center">Task Master</h1>
<table style="border: 1px solid #aaa; border-collapse: collapse; width: 100%;">
<tr style="border: 1px solid #aaa; height: 30px;">
<!-- aqui criamos a tabela e rows-->
<th>Task</th>
<th>Added</th>
<th>Action</th>
</tr>
{% for task in tasks %}
<tr>
<td style="text-align: center; padding: 5px;">{{ task.content }}</td>
<td style="text-align: center; padding: 5px;">{{ task.date_created.date() }}</td>
<td style="text-align: center; padding: 5px;">
<a href="/delete/{{task.id}}">Delete</a>
<br>
<a href="/update/{{task.id}}">Update</a>
</td>
</tr>
{% endfor %}
</table>
<form style="margin-top: 20px;" action="/" method="POST">
<input type="text" name="content" id="content">
<input type="submit" value="Add task">
</form>
</div>
{% endblock %} {% endblock %}

18
templates/update.html Normal file
View File

@@ -0,0 +1,18 @@
{% extends 'base.html' %}
{% block head %}
<title>Fazer coisas</title>
{% endblock %}
{% block body %}
<div class="content">
<h1 style="text-align: center">Update Master</h1>
<div class="form">
<form style="margin-top: 20px;" action="/update/{{task.id}}" method="POST">
<input type="text" name="content" id="content" value="{{task.content}}">
<input type="submit" value="Update task">
</form>
</div>
</div>
{% endblock %}

BIN
test.db Normal file

Binary file not shown.