-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.py
50 lines (36 loc) · 1.66 KB
/
todo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, render_template,redirect,url_for,request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////Python Uygulamaları/Web_Uygulamalari/TodoApp/todo.db'
db = SQLAlchemy(app)
@app.route("/")
def index():
todos = Todo.query.all() # oluşturulan todo class'ında oluşturulan tablodaki tüm veriler alındı. index.html kısmında for döngüsü ile tabloya eklendi
return render_template("index.html",todos = todos)
@app.route("/complete/<string:id>")
def completeTodo(id):
todo = Todo.query.filter_by(id = id).first() #filtreleme işlemi yapılarak seçilen id'deki veri alındı
todo.complete = not todo.complete
db.session.commit()
return redirect(url_for("index"))
@app.route("/delete/<string:id>")
def deleteTodo(id):
todo = Todo.query.filter_by(id = id).first()
db.session.delete(todo)
db.session.commit()
return redirect(url_for("index"))
@app.route("/add", methods = ["POST"])
def addTodo():
title = request.form.get("title") #index.html dosyasında name=title olan yerdeki bilgiyi çektik
newTodo = Todo(title = title, complete = False) #oluşturulan todo class'ının objesi oluşturuldu
db.session.add(newTodo) #oluşturulan newTodo objesi sql tablosuna eklendi
db.session.commit() #veritabanında değişiklik yapılacağı zaman bu commit yazılmalıdır.
return redirect(url_for("index"))
# Veritabanı oluşturma
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
complete = db.Column(db.Boolean)
if __name__ == "__main__":
db.create_all()
app.run(debug=True)