I uploaded the Uploading Files document for photo albums in the project. İmage upload is not working.
Python Code :
UPLOAD_FOLDER = '/static/uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#Album form
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/admin/fotografAlbumu", methods=["GET", "POST"])
@login_required
def upload():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['photo']
# 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))
import pdb;pdb.set_trace()
return redirect(url_for('uploaded_file', filename=filename))
return render_template('/admin/galeri.html')
HTML Code :
<form method="post" class="col-12" enctype="multipart/form-data">
<ul class="row">
<li class="col-xs-6 col-lg-12" style="border: none;">
<div class="form-group">
<input id="input-id" type="file" name="photo" class="file form-control" multiple data-preview-file-type="text" data-allowed-file-extensions='["png", "jpg"]'>
</div>
</li>
</ul>
<button type="submit" class="btn form-control btn-default">Kaydet</button>
</form>
Output : When I select images on the page and upload them, they redirect to the same page and the images are not loaded. What do I have to do? I'm working on 2 - 3 days, but I can not find a solution.