-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
41 lines (30 loc) · 1019 Bytes
/
Copy pathapp.py
File metadata and controls
41 lines (30 loc) · 1019 Bytes
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
from flask import render_template, request, redirect
from constants import app, db
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(300), nullable=False)
text = db.Column(db.Text, nullable=False)
@app.route('/index')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/posts')
def posts():
posts = Post.query.all()
return render_template('/posts.html', posts=posts)
@app.route('/create', methods = ['POST', 'GET'])
def create():
if request.method == 'POST':
title = request.form['title']
text = request.form['text']
post = Post(title = title, text = text )
try:
db.session.add(post)
db.session.commit()
return redirect('/')
except:
return 'при добавлении статьи возникла ошибка'
else:
return render_template('create.html')
if __name__ == '__main__':
app.run(debug=True)