【问题标题】:How to get a new object after form submittig?表单提交后如何获取新对象?
【发布时间】:2015-10-20 21:16:54
【问题描述】:

我正在尝试烧瓶,但提交表单的任务很简单。 页面显示一张图片和一张表格,如果表格提交正确,则应更改图片,否则 - 相同。 我无法理解如何在页面上仅显示一个对象并在提交表单后获取另一个对象的机制。 尝试在图像列表上使用迭代器是文件夹“静态”,但我的实现无法正常工作。 请给我一个反馈,如何正确地做到这一点?

现在我有了简单的看法:

@app.route("/", methods=["GET", "POST"])
def start_view():
    picture = None
    form = InputForm(csrf_enabled=False)
    if form.validate_on_submit():
        picture = form.picture.data
        form.picture.data = ""

    return render_template('04-2.html', form=form, picture=picture)

class InputForm(Form):
    picture = StringField('What is on a picture?', validators[DataRequired()])
    submit = SubmitField('Submit')

还有一个简单的模板:

<body>
    <form method="POST">
    {{ form.picture.label }} {{ form.picture }}
    {{ form.submit() }}
    </form>
    {% if form.errors %}
        <span style="color: red">{{ form.error }}</span>
    {% endif %}
</body>

谢谢!

【问题讨论】:

    标签: forms python-3.x flask jinja2


    【解决方案1】:

    您的表单不包含任何图片。它有一个StringField 和一个SubmitField。如果您想查看任何图像,您需要在 HTML 中使用 &lt;img&gt; 标记指向服务器中的图像位置 您的视图应如下所示:

    from Flask import session
    # in order to use sessions you have to use a secret key for your app
    app.secret_key = 'some secret key'
    @app.route("/", methods=["GET", "POST"])
    def start_view():
        img_list = ['filename1', 'filename2', 'filename3']
    
        # if this is not the first form submission
        if session.has_key('current'):
            # if we reach the end of the list show the first image again
            if int(session['current']) == len(img_list) - 1:
                session['current'] = 0
            # move to next image
            else:
                session['current'] = int(session['current']) + 1
        else:
            session['current'] = 0
    
        picture = 'first_image_filename' # this should be the img on load
        form = InputForm(csrf_enabled=False)
        if form.validate_on_submit():
            picture = img_list[int(session['current'])] # the filename of the next image
            form.picture.data = ""
    
        return render_template('04-2.html', form=form, picture=picture)
    

    所以模板应该是这样的:

    <body>
        <form method="POST">
            {{ form.picture.label }} {{ form.picture }}
            <img src="{{url_for('static', filename='img/' + picture)}}"
            {{ form.submit() }}
        </form>
    {% if form.errors %}
        <span style="color: red">{{ form.error }}</span>
    {% endif %}
    </body>
    

    【讨论】:

    • 谢谢,但是显示图片不是问题,当我有超过2张图片时如何处理?
    • 如果您的图像集是有限的,我认为这是因为您没有指定任何用户图像上传,为什么不列出这些图像文件名,然后从该列表中选择您想要的任何图像?
    • 这是一个问题,提交后如何从列表中拍摄一张又一张的照片,以此类推
    • 如果您只提交一次表单,那么在提交表单后渲染页面时,您只能选择一张图片显示。如果要在每次提交表单后切换图片,可以考虑使用会话保存图片列表的索引位置,这样每次表单提交后都会显示列表中的下一张图片
    • 谢谢!您能否更新您的答案以在我的情况下使用会话?我会以它为例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多