-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (48 loc) · 1.93 KB
/
main.py
File metadata and controls
72 lines (48 loc) · 1.93 KB
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
from flask import Flask, request, redirect, render_template, flash
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 Blog(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
body = db.Column(db.String(1200))
def __init__(self, title, body):
self.title = title
self.body = body
@app.route('/')
def index():
return redirect('/blog')
@app.route('/newpost', methods=['POST', 'GET'])
def newpost():
if request.method == 'POST':
blog_title = request.form['title']
blog_body = request.form['body']
body_error = ''
title_error = ''
if not blog_title:
title_error = 'Title must contain text.'
if not blog_body:
body_error = 'Body must contain text.'
if not body_error and not title_error:
new_post = Blog(blog_title, blog_body )
db.session.add(new_post)
db.session.commit()
return redirect('/blog?id{}'.format(new_post.id))
else:
return render_template('new_post.html', title='New Entry', title_error=title_error, body_error=body_error, blog_title=blog_title, blog_body=blog_body)
return render_template('new_post.html', title='new_post.id')
@app.route('/blog', methods=['POST', 'GET'])
def blog():
blog_id = request.args.get('id')
if blog_id == None :
blogs = Blog.query.all()
return render_template('blog.html', blogs=blogs, title='Build-a-Blog')
else:
blog = Blog.query.get(blog_id)
return render_template('entry.html', blog=blog, title='Blog Entry')
return render_template('blog.html')
if __name__ == '__main__':
app.run()