关于 CKEditor 上传图片的问题~~~

flask-ckeditor

#1

李老师我在配置的时候出现这个问题:
代码截图:

# views.py
@main.route('/new',methods=['GET', 'POST'])
def new():
    """View function for new_port."""
    form = PostForm()
    if form.validate_on_submit():
        #new_post = Post(title=form.title.data)
        #title = form.title.data
        #new_post.body = form.body.data
        # new_post.publish_date = datetime.now()
        #db.session.add(new_post)
        #db.session.commit()
        flash('提交成功','success')
        #return redirect(url_for('.lists'))

    return render_template('xxy/post.html',form=form)
@main.route('/files/<filename>')
def uploaded_files(filename):
    path = current_app.config['UPLOAD_FOLDER']
    return send_from_directory(path, filename)



@main.route('/upload', methods=['POST'])
def upload_images():
    path = current_app.config['UPLOAD_FOLDER']
    f = request.files.get('upload')  # 获取上传图片文件对象
    # Add more validations here
    if extension not in ['jpg', 'gif', 'png', 'jpeg']:  # 验证文件类型示例
        return upload_fail(message='Image only!')  # 返回upload_fail调用
    f.save(os.path.join(path, f.filename))
    url = url_for('uploaded_files', filename=f.filename)
    return upload_success(url=url) # 返回upload_success调用

# config.py

    CKEDITOR_SERVE_LOCAL = True
    CKEDITOR_PKG_TYPE = 'standard'
    CKEDITOR_LANGUAGE = 'zh-CN'
    CKEDITOR_HEIGHT = 600
    CKEDITOR_WIDTH = 300
    CKEDITOR_FILE_UPLOADER = 'upload_images'
    CKEDITOR_ENABLE_CSRF = True
    UPLOAD_FOLDER = os.path.join(basedir, 'uploads')
    ALLOWED_EXTENSIONS = {'png', 'jpg', 'gif', 'jpeg'}
    MAX_CONETNT_LENGTH = 16 * 1024 * 1024

# post.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask-CKEditor Demo</title>
</head>
<body>
        <div class="warpper" style="width: 700px; margin: auto">
                <h1>Flask-CKEditor Demo: Image Upload</h1>
                <form method="post" ">
                        {{ form.csrf_token }}
                        {{ form.title.label }}<br>
                        {{ form.title }}<br><br>
                        {{ form.body.label }}<br>
                        {{ form.body }}
                        <br>
    					{{ form.submit(class="btn btn-primary") }}
                </form>
        </div>
    {{ ckeditor.load() }}
    {{ ckeditor.config(name='body') }}
</script>
</body>
</html>

我这边使用的是蓝图 ,分了auth,跟main两个蓝图。主要的函数都写在main这里面的。
报错如下,烦请问下是哪里出问题了,谢谢。
image


#2

蓝本的端点是有命名空间的,在传入 url_for 的时候需要加上蓝本的名称,形式是蓝本名.视图函数名。如果你的 upload 视图函数是定义在 main 蓝本,那么构建 URL 的方式就是:

url_for('main.upload')

在 main 蓝本的视图函数内或是对应的模板里,可以简写成:

url_for('.upload')  # 注意开头的点

但不能写成:

url_for('upload')

下次建议使用文本给出错误信息,截图不方便复制,而且容易截不完整(具体的代码出错行数大概在 main/views.py 77 行,具体见错误回溯)。