-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (48 loc) · 1.84 KB
/
main.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
from flask import Flask, request, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://build-a-blog:build-a-blog@localhost:8889/build-a-blog'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
class Entry(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200))
body = db.Column(db.String(2000))
def __init__(self, name, body):
self.name = name
self.body = body
@app.route('/')
def index():
blogPosts = Entry.query.all()
return render_template('index.html', title="Living Diary", posts=blogPosts)
@app.route('/entry')
def entry():
return render_template('entry.html', title="Make a Post")
@app.route('/posted', methods=['POST', 'GET'])
def posted():
if request.method == 'POST':
postName = request.form['name']
postBody = request.form['body']
errName = ''
errBody = ''
if postName == '' or postBody == '':
if postName == '':
errName = "Text Required"
if postBody == '':
errBody = "Text Required"
return render_template('entry.html', title="Make a Post",
postName=postName, postBody=postBody, errName=errName, errBody=errBody)
newPost = Entry(postName, postBody)
db.session.add(newPost)
db.session.commit()
postQuery = Entry.query.get(newPost.id)
return render_template('posted.html', title=postName, postName=postName, postBody=postBody,
postQuery=postQuery)
else:
postId = int(request.args.get('id'))
postQuery = Entry.query.get(postId)
Name = postQuery.name
return render_template('posted.html', title=Name, postQuery=postQuery)
if __name__ == "__main__":
app.run()