загрузка файлов при создании поста и вставка картинок динамически с загрузкой в папку uploads

main
Сергей Ванюшкин 2023-10-11 14:43:58 +03:00
parent 3785ab37e6
commit 911d3ef845
5 changed files with 29 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
pyproger/upload/*
config.ini config.ini
data/ data/

View File

@ -1,9 +1,10 @@
import os import os
from dotenv import load_dotenv 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_admin import helpers
from flask_ckeditor import CKEditor from flask_ckeditor import CKEditor, upload_fail, upload_success
from flask_migrate import Migrate from flask_migrate import Migrate
from flask_security.core import Security from flask_security.core import Security
@ -168,4 +169,19 @@ def create_app(test_config=None):
def hello(): def hello():
return render_template_string("pong") 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 return app

View File

@ -40,7 +40,11 @@ SECURITY_SEND_PASSWORD_RESET_NOTICE_EMAIL = False
LANGUAGES = ["ru"] LANGUAGES = ["ru"]
BABEL_TRANSLATION_DIRECTORIES = os.path.join(os.path.curdir, "translations") BABEL_TRANSLATION_DIRECTORIES = os.path.join(os.path.curdir, "translations")
basedir = os.path.abspath(os.path.dirname(__file__))
CKEDITOR_PKG_TYPE = "full" CKEDITOR_PKG_TYPE = "full"
CKEDITOR_SERVE_LOCAL = True CKEDITOR_SERVE_LOCAL = True
CKEDITOR_ENABLE_CODESNIPPET = True CKEDITOR_ENABLE_CODESNIPPET = True
CKEDITOR_CODE_THEME = "monokai_sublime" 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")

View File

@ -1,6 +1,12 @@
{% extends 'admin/model/edit.html' %} {% extends 'admin/model/edit.html' %}
{% block head %}
{{ super()}}
{{ ckeditor.load_code_theme() }}
{%endblock%}
{% block tail %} {% block tail %}
{{ super() }} {{ super() }}
{{ ckeditor.load() }} {{ ckeditor.load() }}
{{ ckeditor.config(name='text')}}
{% endblock %} {% endblock %}

View File