загрузка файлов при создании поста и вставка картинок динамически с загрузкой в папку uploads
parent
3785ab37e6
commit
911d3ef845
|
@ -1,3 +1,4 @@
|
|||
pyproger/upload/*
|
||||
config.ini
|
||||
data/
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from flask import Flask, render_template_string, url_for
|
||||
from flask import (Flask, render_template_string, request, send_from_directory,
|
||||
url_for)
|
||||
from flask_admin import helpers
|
||||
from flask_ckeditor import CKEditor
|
||||
from flask_ckeditor import CKEditor, upload_fail, upload_success
|
||||
from flask_migrate import Migrate
|
||||
from flask_security.core import Security
|
||||
|
||||
|
@ -168,4 +169,19 @@ def create_app(test_config=None):
|
|||
def hello():
|
||||
return render_template_string("pong")
|
||||
|
||||
@app.route("/files/<filename>")
|
||||
def uploaded_files(filename):
|
||||
path = app.config["UPLOADED_PATH"]
|
||||
return send_from_directory(path, filename)
|
||||
|
||||
@app.route("/upload", methods=["POST"])
|
||||
def upload():
|
||||
f = request.files.get("upload")
|
||||
extension = f.filename.split(".")[-1].lower()
|
||||
if extension not in ["jpg", "gif", "png", "jpeg"]:
|
||||
return upload_fail(message="Image only!")
|
||||
f.save(os.path.join(app.config["UPLOADED_PATH"], f.filename))
|
||||
url = url_for("uploaded_files", filename=f.filename)
|
||||
return upload_success(url=url)
|
||||
|
||||
return app
|
||||
|
|
|
@ -40,7 +40,11 @@ SECURITY_SEND_PASSWORD_RESET_NOTICE_EMAIL = False
|
|||
LANGUAGES = ["ru"]
|
||||
BABEL_TRANSLATION_DIRECTORIES = os.path.join(os.path.curdir, "translations")
|
||||
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
CKEDITOR_PKG_TYPE = "full"
|
||||
CKEDITOR_SERVE_LOCAL = True
|
||||
CKEDITOR_ENABLE_CODESNIPPET = True
|
||||
CKEDITOR_CODE_THEME = "monokai_sublime"
|
||||
CKEDITOR_FILE_UPLOADER = "upload"
|
||||
# app.config['CKEDITOR_ENABLE_CSRF'] = True # if you want to enable CSRF protect, uncomment this line
|
||||
UPLOADED_PATH = os.path.join(basedir, "uploads")
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
{% extends 'admin/model/edit.html' %}
|
||||
|
||||
{% block head %}
|
||||
{{ super()}}
|
||||
{{ ckeditor.load_code_theme() }}
|
||||
{%endblock%}
|
||||
|
||||
{% block tail %}
|
||||
{{ super() }}
|
||||
{{ ckeditor.load() }}
|
||||
{{ ckeditor.config(name='text')}}
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in New Issue