-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyblog.py
66 lines (55 loc) · 2.01 KB
/
myblog.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
# encoding=utf-8
from flask import Flask,render_template,request,redirect, url_for
from lib.model import db_session,engine
from datetime import *
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
admin_name = 'admin'
admin_pass = 'admin'
app = Flask(__name__)
@app.route('/')
def index():
result = db_session.execute('select * from aj_articles order by id desc limit 10 ', {})
resultlist = result.fetchall()
artlices=[]
for row in resultlist:
print '______________'
rowinfo = dict(zip(row.keys(), row.values()))
artlices.append(rowinfo)
return render_template('index.html', artlices=artlices)
@app.route('/detail/<int:id>')
def detail(id):
result = db_session.execute('select * from aj_articles where id = :id', {'id':id})
article = dict(result.first())
return render_template('detail.html', item=article)
@app.route('/admin',methods=['POST', 'GET'])
def login():
error = None
if request.method == 'POST':
username = request.form['username']
userpass = request.form['userpass']
if (username == admin_name) and (userpass == admin_pass):
return redirect(url_for('post'))
else:
error = 'Invalid username/password'
return render_template('login.html',error=error)
@app.route('/admin/post',methods=['POST', 'GET'])
def post():
error = None
author='aj'
dt = datetime.now()
posttime = dt.strftime('%Y-%m-%d %I:%M:%S')
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if title and content:
db_session.execute('insert into aj_articles (title,content,posttime,author) values(:title,:content,:posttime,:author)', {'title':title,'content':content,'posttime':posttime,'author':author})
db_session.commit()
error = 'Post Success'
else:
error = 'Invalid username/password'
return render_template('post.html',error=error)
if __name__ == '__main__':
app.debug = True
app.run()