【发布时间】:2020-12-19 21:58:24
【问题描述】:
我正在尝试使用 Flask 将 tinyMce 集成到博客应用程序中。我在编辑器中正确上传和显示了图片,但是当用户想要阅读整篇文章时,我的图片没有显示。
* Serving Flask app "blog.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
[2020-08-31 19:08:56,321] INFO in __init__: Blog site startup.
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
...
127.0.0.1 - - [31/Aug/2020 19:09:07] "POST /image-upload HTTP/1.1" 200 -
Validating Image....[OK]
/static/user_uploads/img_0992.jpeg
127.0.0.1 - - [31/Aug/2020 19:09:37] "POST /create-post HTTP/1.1" 302 -
127.0.0.1 - - [31/Aug/2020 19:09:37] "GET /index HTTP/1.1" 200 -
127.0.0.1 - - [31/Aug/2020 19:09:37] "GET /jsglue.js HTTP/1.1" 200 -
127.0.0.1 - - [31/Aug/2020 19:09:40] "GET /post/1 HTTP/1.1" 200 -
127.0.0.1 - - [31/Aug/2020 19:09:40] "GET /jsglue.js HTTP/1.1" 200 -
127.0.0.1 - - [31/Aug/2020 19:09:40] "GET /post/static/user_uploads/img_0992.jpeg HTTP/1.1" 404
TinyMCE 初始化:
tinymce.init({
selector: 'textarea',
plugins: [
'advlist autolink link image imagetools lists charmap print preview hr anchor pagebreak spellchecker',
'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking',
'save table contextmenu directionality template paste textcolor codesample'
],
toolbar: 'insertfile undo redo | styleselect | bold italic forecolor | alignleft aligncenter alignright alignjustify | bullist numlist | link image | print preview media fullpage | emoticons',
images_upload_url: Flask.url_for('blog.image_uploader'),
images_reuse_filename: false,
automatic_uploads: true,
});
烧瓶视图:
@blog.route('/image-upload', methods=['POST'])
@login_required
def image_uploader():
"""
Upload post images from tinyMce editor.
Save image to path.
returns: json { location: path }
"""
# Get the file user has uploaded inside the tinymce editor.
uploaded_file = request.files.get('file')
if uploaded_file:
filename = secure_filename(uploaded_file.filename).lower()
# Validate the contents of the file. Check the header of the file is infact an image.
valid_img_ext = validate_img(uploaded_file.stream)
# Split filename and extension, rename & add correct extension.
filename = secure_filename(os.path.splitext(filename)[0] + valid_img_ext)
img_path = os.path.join(current_app.config['IMG_UPLOAD_PATH'], filename)
# Check if user directory exists, create if nessecary.
if not os.path.exists(current_app.config['IMG_UPLOAD_PATH']):
try:
os.makedirs(current_app.config['IMG_UPLOAD_PATH'])
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Save the image.
uploaded_file.save(img_path)
location = url_for('static', filename='user_uploads/' + filename)
print(location)
# Return image path back to editor
return jsonify({'location': location})
abort(Response('404 - Image failed to upload'))
@blog.route('/post/<post_id>')
def post_detail(post_id):
post = Post.query.get(post_id)
return render_template('article.html', post=post)
正如您在上面看到的,图像验证并正确保存。然后我尝试查看这篇文章,我得到一个404 - /post/static/user_uploads/img_0992.jpeg 如何停止以/post/ 为前缀的路径所以我只是得到/static/user_uploads/img_0992.jpeg
例如,改变
@blog.route('/post/<post_id>')
到
@blog.route('/<post_id>')
图片按原样加载,但这不是可行的解决方案
127.0.0.1 - - [31/Aug/2020 19:45:05] "GET /static/user_uploads/img_0992.jpeg HTTP/1.1" 200 -
另外,如果我打开控制台并将 / 添加到 img src,图像会按应有的方式加载。
<img alt="" height="267" src="static/user_uploads/img_0992.jpeg" width="200">
到
<img alt="" height="267" src="/static/user_uploads/img_0992.jpeg" width="200">
我哪里错了?
【问题讨论】: