Skip to content

Instantly share code, notes, and snippets.

@muradddd
Created November 2, 2020 18:29
Show Gist options
  • Save muradddd/46335b7f2c5be806219c7e808d3fdaa7 to your computer and use it in GitHub Desktop.
Save muradddd/46335b7f2c5be806219c7e808d3fdaa7 to your computer and use it in GitHub Desktop.
@core.route('/create', methods=['GET', 'POST'])
@login_required
def create():
form = BlogForm()
if request.method == 'POST' and form.validate_on_submit():
f = form.image.data
file_path = save_file(f)
blog = Blog(title=form.title.data, description=form.description.data, image=file_path, user_id=current_user.id)
db.session.add(blog)
db.session.commit()
flash('Melumat elave edildi')
return redirect('/')
context = {
'form': form
}
return render_template('core/create.html', **context)
from flask import Flask, session, send_from_directory
BASE_DIRS = os.path.dirname(os.path.abspath(__file__))
UPLOADED_FILES_DIR = os.path.join(BASE_DIRS, 'media')
app.config['UPLOAD_FOLDER'] = UPLOADED_FILES_DIR
if not os.path.isdir(UPLOADED_FILES_DIR):
os.mkdir(UPLOADED_FILES_DIR)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(UPLOADED_FILES_DIR,
filename)
from flask import url_for
from blog import UPLOADED_FILES_DIR, os
from werkzeug.utils import secure_filename
from datetime import datetime
def save_file(f):
filename = secure_filename(f.filename)
filename = f'{datetime.now()}_{filename}'
file_dir = os.path.join(
UPLOADED_FILES_DIR, filename
)
f.save(file_dir)
file_path = url_for('uploaded_file', filename=filename) # '/uploads/<filename>'
return file_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment