-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_news.py
129 lines (101 loc) · 3.44 KB
/
flask_news.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# coding=UTF-8
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask import render_template, flash, url_for, redirect, abort, request
from flask import Flask
from wsgiref.simple_server import make_server
from form import NewsForm
app = Flask(__name__)
# 数据库连接属性
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:011022@localhost:3306/news'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = '123456' # 解决报错A secret key is required to use CSRF.
db = SQLAlchemy(app)
class News(db.Model):
""" 新闻模型 """
__tablename__ = 'news'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), nullable=False)
content = db.Column(db.String(2000), nullable=False)
types = db.Column(db.String(10), nullable=False)
image = db.Column(db.String(200), nullable=False)
author = db.Column(db.String(20))
view_count = db.Column(db.Integer)
is_valid = db.Column(db.Boolean, default=True)
created_at = db.Column(db.DateTime)
# def __repr__(self):
# return '<News %r>' % self.title
#
@app.route("/")
def index():
"""新闻首页"""
news_list = News.query.all()
return render_template("index.html", news_list=news_list)
@app.route("/cat/<name>/")
def cat(name):
"""新闻的类别"""
# 查询类别为name的新闻数据
return render_template("cat.html", name=name)
@app.route("/detail/<int:pk>/")
def detail(pk):
"""新闻的类别"""
# 查询类别为name的新闻数据
return render_template("detail.html", pk=pk)
@app.route('/admin/')
@app.route('/admin/<int:page>')
def admin(page=None):
""" 后台管理首页 """
if page is None: # 如果没有传这个参数,默认为第一页
page = 1
page_data = News.query.paginate(page=page, per_page=5)
return render_template("admin/index.html", page_data=page_data)
@app.route('/admin/add/', methods=['GET', 'POST'])
def add():
""" 新增新闻 """
form = NewsForm()
if form.validate_on_submit():
n1 = News(
title=form.title.data,
content=form.content.data,
image=form.img_url.data,
types=form.news_type.data,
created_at=datetime.now(),
)
db.session.add(n1)
db.session.commit()
flash("新增成功")
return redirect(url_for('admin'))
return render_template("admin/add.html",form=form)
@app.route('/admin/update/<int:pk>/', methods=['GET', 'POST'])
def update(pk):
""" 修改新闻 """
obj = News.query.get(pk)
if obj is None:
abort(404)
form = NewsForm(obj=obj)
print(form)
if form.validate_on_submit():
obj.title = form.title.data
obj.content = form.content.data
obj.news_type = form.news_type.data
db.session.add(obj)
db.session.commit()
flash("修改成功")
return redirect(url_for('admin'))
return render_template("admin/update.html", form=form)
@app.route('/admin/delete/<int:pk>/', methods=['POST'])
def delete(pk):
""" 删除新闻 """
if request.method == 'POST':
obj = News.query.get(pk)
if obj is None:
return 'no'
obj.is_valid = False
db.session.add(obj)
db.session.commit()
return 'yes'
return 'no'
if __name__ == "__main__":
server = make_server('127.0.0.1', 5000, app)
server.serve_forever()
app.run(debug=True)