-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
114 lines (83 loc) · 2.82 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from flask import Flask, render_template, url_for, request, redirect, abort
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
app.config['SQlALCHEMY_TRACK_MODIFICATION'] = False
db = SQLAlchemy(app)
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
text = db.Column(db.Text, nullable=False)
date = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Article %r>' % self.id
@app.route('/')
@app.route('/home')
def index():
return render_template("index.html")
@app.route('/posts')
def posts():
articles = Article.query.order_by(Article.date.desc()).all()
return render_template("posts.html", articles=articles)
@app.route('/posts/<int:id>')
def posts_detail(id):
article = Article.query.get(id)
return render_template("post_detail.html", article=article)
@app.route('/posts/<int:id>/del')
def posts_delete(id):
article = Article.query.get_or_404(id)
try:
db.session.delete(article)
db.session.commit()
return redirect("/posts")
except:
return "При удалении статьи произошла ошибка"
@app.route('/agile')
def agile():
return render_template("agile.html")
@app.route('/aqa')
def aqa():
return render_template("aqa.html")
@app.route('/css')
def css():
return render_template("css.html")
@app.route('/git')
def git():
return render_template("git.html")
@app.route('/sql')
def sql():
return render_template("sql.html")
@app.route('/create-article', methods=['POST', 'GET'])
def create_article():
if request.method == "POST":
title = request.form["title"]
text = request.form["text"]
article = Article(title=title, text=text)
try:
db.session.add(article)
db.session.commit()
return redirect('/posts')
except:
return "При добавлении статьи произошла ошибка"
else:
return render_template("create-article.html")
@app.route('/posts/<int:id>/update', methods=['POST', 'GET'])
def post_update(id):
article = Article.query.get(id)
if request.method == "POST":
article.title = request.form["title"]
article.text = request.form["text"]
try:
db.session.commit()
return redirect('/posts')
except:
return "При обновлении статьи произошла ошибка"
else:
return render_template("post_update.html", article=article)
@app.route('/questions')
def questions():
return render_template("questions.html")
if __name__ == "__main__":
app.run(debug=True)