我正在尝试使用Project类构建网页,而Project类将具有多个图像文件。 我可以上传和存储图像文件,但不能将图像的文件路径插入数据库。因此,我可以从db调用图像路径。我有一个sqlite数据库,我使用Flask-SQLAlchemy。 这是我的项目班级。我搞砸了。
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(120), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
description = db.Column(db.Text, nullable=False)
header_image = db.Column(db.String(30), nullable=False, default='default_project.jpg')
project_images = db.relationship('ImageSet', backref='project', lazy=True, uselist=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Project('{self.title}', {self.date_posted}')"
class ImageSet(db.Model):
id = db.Column(db.Integer, primary_key=True)
imageset = db.Column(db.String(120), nullable=False)
project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False)
这是添加项目的途径和功能
def save_picture(form_picture):
random_hex = secrets.token_hex(8)
_, f_ext = os.path.splitext(form_picture.filename)
picture_fn = random_hex + f_ext
picture_path = os.path.join(app.root_path, 'static/images/project_images', picture_fn)
output_size = (1280, 720)
i = Image.open(form_picture)
i.thumbnail(output_size)
i.save(picture_path)
return picture_fn
@app.route('/add_project', methods=["GET", "POST"])
@login_required
def add_project():
form = AddProjectForm()
imageset = ImageSet()
if form.validate_on_submit():
if form.header_image.data:
header_image = save_picture(form.header_image.data)
if form.project_images.data:
for image in form.project_images.data:
image = save_picture(image)
project = Project(title=form.title.data, description=form.description.data, project_images=, user_id=current_user.id)
db.session.add(project)
db.session.commit()
flash('Project Successfully Added', 'succes')
return render_template('add_project.html', title='Add Project', form=form)
return render_template('add_project.html', title='Add Project', form=form)
答案 0 :(得分:0)
由于@JosefKorbel,我已经找到了解决方案。 对于任何可能需要解决问题的人:
我在课堂上使用了一对多关系。
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(120), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
description = db.Column(db.Text, nullable=False)
header_image = db.Column(db.String(30), nullable=False, default='default_project.jpg')
project_images = db.relationship('ImageSet', backref='project_images', lazy=True, uselist=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Project('{self.title}', {self.date_posted}')"
class ImageSet(db.Model):
id = db.Column(db.Integer, primary_key=True)
image = db.Column(db.String(120), nullable=False)
project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False)
路线,图像保存功能:
def save_picture(form_picture):
random_hex = secrets.token_hex(8)
_, f_ext = os.path.splitext(form_picture.filename)
picture_fn = random_hex + f_ext
picture_path = os.path.join(app.root_path, 'static/images/project_images', picture_fn)
output_size = (1280, 720)
i = Image.open(form_picture)
i.thumbnail(output_size)
i.save(picture_path)
return picture_fn
@app.route('/add_project', methods=["GET", "POST"])
@login_required
def add_project():
form = AddProjectForm()
if form.validate_on_submit():
if form.header_image.data:
header_image = save_picture(form.header_image.data)
project = Project(title=form.title.data, description=form.description.data, header_image=header_image, user_id=current_user.id)
if form.project_images.data:
for image in form.project_images.data:
image = save_picture(image)
project.project_images.append(ImageSet(image=image))
time.sleep(2)
db.session.add(project)
db.session.commit()
flash('Project Successfully Added', 'succes')
return render_template('add_project.html', title='Add Project', form=form)
return render_template('add_project.html', title='Add Project', form=form)
在处理每个图像之前,我使用time.sleep(2)
是因为sqlite在不等待的情况下会锁定数据库。