-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
82 lines (58 loc) · 2.38 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
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
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:launchcode@localhost:8889/build-a-blog'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
app.secret_key = "$$#^^&*JNFDFVRG("
#Create Blog class and a constructor with title and body parameters
class Blog(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(120))
body = db.Column(db.String(250))
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':
title = request.form['title']
body = request.form['body']
new_blog = Blog(title, body)
db.session.add(new_blog)
db.session.commit()
return render_template("newpost.html", title ="Create something assho--!")
@app.route("/blog", methods = ['POST', 'GET'])
def blog():
if request.method == 'POST':
blog_title= request.form['title']
blog_body= request.form['body']
title_error = ""
body_error = ""
if blog_title == "":
title_error = "Please add a blog title."
if blog_body =="":
body_error = "Please add some content to the new blog post."
#messages = [blog_title,blog_body, body_error, title_error]
if title_error or body_error:
return render_template("newpost.html", blog_title = blog_title, blog_body = blog_body,
title_error = title_error, body_error = body_error)
else:
new_blog = Blog(blog_title, blog_body)
db.session.add(new_blog)
db.session.commit()
return render_template("single_entry.html", blog = new_blog)
else:
is_blog_id = request.args.get('id')
if is_blog_id:
single_blog = Blog.query.filter_by(id = is_blog_id).first()
return render_template("single_entry.html", blog = single_blog)
else:
blogs = Blog.query.all()
return render_template("all_entries.html", blogs = blogs, title = "MiBlogs")
if __name__ == "__main__":
app.run()