我正在建立一个网站,人们应该可以添加带图片的小插件。所有图片都会转到同一目录。所以我需要重命名图片,这样就不会有任何重写或冲突。
当我向网站添加添加时,它会自动增加add_id。我想将此add_id用于图片名称。
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/aufgeben', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
titel = request.form['titel']
beschreibung = request.form['beschreibung']
kategorie = request.form['kategorie']
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Anzeigeninhalt in DB eintragen
cursor = g.con.cursor()
cursor.execute('insert into anzeigen (titel, beschreibung, kategorie) values (%s, %s, %s)',
(titel, beschreibung, kategorie))
g.con.commit()
cursor.close()
flash("Anzeige aufgegeben.")
return render_template('aufgeben.html',)
感谢您的帮助。也许有人有更好的想法如何解决这个问题?