简体   繁体   English

如何将 flask 预览中的元素集成到评论中

[英]How to integrate the elements from a flask preview into a comment

In my requirements.txt I have: Flask 2.0.3, Flask_Pagedown 0.4.0, Flask-wtf 1.0.1, Jinja2 3.0.3 WTForms 3.0.0, etc. The first code is a view for previewing texts where I find it from Github https://github.com/miguelgrinberg/Flask-PageDown/blob/main/example/app.py#L20 , while the second one is a comment that is working perfectly with quill editor.在我的 requirements.txt 中,我有:Flask 2.0.3、Flask_Pagedown 0.4.0、Flask-wtf 1.0.1、Jinja2 3.0.3 WTForms 3.0.0 等。第一个代码是用于预览文本的视图,我从中找到它Github https://github.com/miguelgrinberg/Flask-PageDown/blob/main/example/app.py#L20 ,而第二个是与 quill 编辑器完美配合的评论。

To get preview before submit for comments, I should find a way to integrate them as one view in my flask_app.py and include other elements in.py and.html.为了在提交评论之前获得预览,我应该找到一种方法将它们作为一个视图集成到我的 flask_app.py 中,并将其他元素包含在 .py 和 .html 中。 Both codes and scripts are working well when running in a separate template.在单独的模板中运行时,代码和脚本都运行良好。

1st codes: view flask-pagedown working codes from miguelgrinberg第一个代码:查看来自 miguelgrinberg 的 flask-pagedown 工作代码

@app.route('/', methods=['GET', 'POST'])
def index():
    form = PageDownFormExample()
    text = None
    text2 = None
    if form.validate_on_submit():
        text = form.pagedown.data
        text2 = form.pagedown2.data
    else:
        form.pagedown.data = ('# This is demo #1 of Flask-PageDown\n'
                              '**Markdown** is rendered on the fly in the '
                              '<i>preview area below</i>!')
        form.pagedown2.data = ('# This is demo #2 of Flask-PageDown\nThe '
                               '*preview* is rendered separately from the '
                               '*input*, and in this case it is located above.')
    return render_template('index.html', form=form, text=text, text2=text2)

Into 2nd codes: view comment working codes进入第二个代码:查看评论工作代码

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == "GET":
        return render_template("main_page.html", comments=Comment.query.all())
    if not current_user.is_authenticated:
        return redirect(url_for('index'))
    comment = Comment(content=request.form["contents"], commenter=current_user)
    db.session.add(comment)
    db.session.commit()
    return redirect(url_for('index'))

What I have tried so far is to put the first instance after method "GET" like this:到目前为止我尝试过的是将第一个实例放在方法“GET”之后,如下所示:

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("main_page.html", comments=Comment.query.all())        
        form = PageDownFormExample()
        text = None
        text2 = None
        if form.validate_on_submit():
            text = form.pagedown.data
            text2 = form.pagedown2.data
        else:
            form.pagedown.data = ('# This is demo #1 of Flask-PageDown\n'
                '**Markdown** is rendered on the fly in the '
                '<i>preview area below</i>!')
            form.pagedown2.data = ('# This is demo #2 of Flask-PageDown\nThe'
                '<i>preview area below</i>!'
                '*input*, and in this case it is located above.')
        return render_template('preview.html', form=form, text=text, text2=text2)
    if not current_user.is_authenticated:
        return redirect(url_for('index'))
    comment = Comment(content=request.form["contents"], commenter=current_user)
    db.session.add(comment)
    db.session.commit()
    return redirect(url_for('index'))

The html error from the above experiment is here:上面实验的 html 错误在这里:

            <div>
                <h1>Flask-Preview Page</h1>
                {% if text or text2 %}
                <div>
                    <p>Received text was:</p>
                    <pre>{{ text }}</pre>
                    <pre>{{ text2 }}</pre>
                </div>
                <hr>
                {% endif %}
                <div style="width: 800px;">
                    <form method="POST">
                        {{ form.hidden_tag() }} <!-- ---------------- error from here-->
                        <div>
                            <b>{{ form1.pagedown.label }}</b>:
                            {{ form.pagedown(rows=10, style='width:100%') }}
                        </div>
                        <br>
                        <div>
                            {{ form.pagedown2(only_preview=True) }}
                            <br>
                            <b>{{ form.pagedown2.label }}</b>:
                            {{ form.pagedown2(only_input=True, rows=10, style='width:100%') }}
                        </div>
                        <div>{{ form.submit() }}</div> <!-- ------------ error up to here------------- -->
                    </form>
                </div>
            </div>
            <div class="row">
                <form action="." method="POST">
                    <div>
                        <label for="contents">Content</label>
                        <input type="hidden" name="contents" value="<?= html_escape($contents) ?>">
                        <div id="editor" style="min-height: 160px;"><?= $contents ?>
                    </div>
                    <div>
    <button type="submit" name="draft" class="btn btn-success">Post Comment Enable</button>
    </div>
                </form>
<script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>
</script>
            </div>

The error start from {{ form.hidden_tag() }} to {{ form.submit() }}.错误从 {{ form.hidden_tag() }} 开始到 {{ form.submit() }}。 When I delete these error codes and 'in between', there are no error but it only appears "Flask-Preview Page" on top of the quill editor without previews.当我删除这些错误代码和“介于两者之间”时,没有错误,但它只在没有预览的情况下出现在羽毛笔编辑器顶部的“Flask-Preview Page”。 When not taking out these codes, the Error log is: jinja2.exceptions.UndefinedError: 'form'不取出这些代码时,错误日志为: jinja2.exceptions.UndefinedError: 'form'

Any help?有什么帮助吗?

If the HTML in the 4th code block is from main_page.html, then the error comes because you never let form object be defined.如果第 4 个代码块中的 HTML 来自 main_page.html,那么错误就会出现,因为您从未定义表单 object。 In your third block of python code, if the method is GET, you return a rendered main_page.html before you instantiate the form object. If the method is POST, only the code from if not current_user.is_authenticated: on gets executed.在 python 代码的第三块中,如果方法是 GET,则在实例化表单 object 之前返回呈现的 main_page.html。如果方法是 POST,则仅执行来自if not current_user.is_authenticated: on 的代码。

Maybe try something like this and see if it's any better:也许尝试这样的事情,看看它是否更好:

@app.route("/", methods=["GET", "POST"])
def index():
    form = PageDownFormExample()
    if request.method == "GET":
        return render_template("main_page.html", comments=Comment.query.all(), form=form)        
    text = None
    text2 = None
    if form.validate_on_submit():
        text = form.pagedown.data
        text2 = form.pagedown2.data
    else:
        form.pagedown.data = ('# This is demo #1 of Flask-PageDown\n'
            '**Markdown** is rendered on the fly in the '
            '<i>preview area below</i>!')
        form.pagedown2.data = ('# This is demo #2 of Flask-PageDown\nThe'
            '<i>preview area below</i>!'
            '*input*, and in this case it is located above.')
    return render_template('preview.html', form=form, text=text, text2=text2)
    if not current_user.is_authenticated:
        return redirect(url_for('index'))
    comment = Comment(content=request.form["contents"], commenter=current_user)
    db.session.add(comment)
    db.session.commit()
    return redirect(url_for('index'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM