flask完整一道题

这篇博客介绍了一种使用Flask框架和SQLAlchemy库来创建一个包含分类和新闻管理的Web应用的方法。应用涉及到数据库模型如分类(Cate)和新闻(News),以及文件上传功能。通过路由处理添加分类、添加新闻、图片上传和页面展示等功能。
摘要由CSDN通过智能技术生成

from flask import *
from flask_sqlalchemy import SQLAlchemy
#导入captcha 用于生成验证码
from captcha.captcha import captcha
#图文混排导包
from flask_uploads import UploadSet, IMAGES, configure_uploads
import sys
import os
import re #正则

app = Flask(name)
app.secret_key = ‘1812B1234567890’
app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘mysql+pymysql://root:@127.0.0.1:3306/gaoqian’
db = SQLAlchemy(app)

fn = getattr(sys.modules[‘main’], ‘file’)
root_path = os.path.abspath(os.path.dirname(fn)) + “/static/upload”
app.config[‘UPLOADED_PHOTO_DEST’] = root_path
app.config[‘UPLOADED_PHOTO_ALLOW’] = IMAGES
photos = UploadSet(‘PHOTO’) # 用来保存图片的对象 photos.save()
configure_uploads(app, photos)

#分类表
class Cate(db.Model):
tablename = ‘cate’
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(30),unique=True,nullable=False)
image = db.Column(db.String(255))
new = db.relationship(‘News’,backref=‘cate’,lazy=‘dynamic’)

#新闻表
class News(db.Model):
tablename = ‘news’
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(30),unique=True,nullable=False)
content = db.Column(db.String(255))
cid = db.Column(db.Integer,db.ForeignKey(‘cate.id’))

db.create_all()

#添加分类
@app.route(’/addcate’,methods=[‘POST’,‘GET’])
def addcate():
if request.method == ‘POST’:
data = request.form
name = data[‘name’]
image = request.files.get(‘image’)
filename = ‘’
if image:
filename = ‘/static/upload/’+photos.save(image)
cate = Cate(name=name,image=filename)
db.session.add(cate)
db.session.commit()
flash(“添加成功”)
return render_template(“addcate.html”)

#添加新闻
@app.route(’/addnews’,methods=[‘POST’,‘GET’])
def addnews():
if request.method == ‘POST’:
data = request.form
name = data[‘name’]
content = data[‘content’]
cid = data[‘cid’]
if not all([name,content,cid]):
flash(“请输入完整”)
else:
news = News(name=name,content=content,cid=cid)
db.session.add(news)
db.session.commit()
flash(“添加成功”)
cate = Cate.query.all()
return render_template(“addnews.html”,cate=cate)

#获取图片
@app.route(’/upload_image’,methods=[‘POST’])
def upload_image():
image = request.files[‘file’]
image_name = photos.save(image)
image_url = ‘/static/upload/’+image_name
mes = {}
mes[‘path’] = image_url
mes[‘error’] = False
return jsonify(mes)

#首页
@app.route(’/’)
def index():
cate = Cate.query.all()
news = News.query.all()
return render_template(“index.html”,cate=cate,news=news)

#详情页
@app.route(’/detail’)
def detail():
id = request.args.get(‘id’)
news = News.query.filter(News.cid==id).all()
return render_template(‘detail.html’,news=news)

if name == ‘main’:
app.run(debug=True)


模板


在这里插入图片描述
addcate.html

首页 添加分类 {% for i in cate %} {% endfor %}

{{i.name}}

{% for c in news %} {% if c.cid==i.id %}

addnews.html

添加内容 {% for mes in get_flashed_messages() %} {​{ mes }} {% endfor %} 请输入新闻: 内容: 分类: {% for i in cate %} {​{i.name}} {% endfor %} 添加

index.html

首页 添加分类 {% for i in cate %} {% endfor %}

{{i.name}}

{% for c in news %} {% if c.cid==i.id %}

detail.html

详情 {% for i in news %} {% endfor %}
{{i.content | safe}}
  • {{i.name}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值