如何从HTML模板中的jinja2循环中进行request.form

时间:2019-04-24 19:51:24

标签: python flask jinja2

因此,我在我的网站上有一个供稿,其中列出了最近制定的所有新“建议”。 这些在我的html模板的jinja2循环中列出。问题是,我在每个提案下都有评分按钮,我希望能够获得用户在python中单击的评分,然后将其发送到数据库。 但是我还是个初学者,我真的不知道该怎么做。

这就是我的html模板中的内容:

            {% for proposition in propositions %}
            <div class="feed_propBox"> <!--prop Box: Begin-->
                <form method="POST" action="rate">

                    <p  id="titre" name="titre" value="{{proposition.title}}">{{ proposition.title }}</p>
                    <p  id="perso" name="perso">{{ proposition.date }}</p>
                    <p  id="content" name="content">{{ proposition.content }}</p>

                    <div class="ratingBlock">
                        <button type="submit" class="mThree ratingButton" id="m3" name="rate" value="m3">-3</button>
                        <button type="submit" class="mTwo ratingButton" id="m2" name="rate" value="m2">-2</button>
                        <button type="submit" class="mOne ratingButton" id="m1" name="rate" value="m1">-1</button>
                        <button type="submit" class="zero ratingButton" id="z" name="rate" value="z">0</button>
                        <button type="submit" class="pOne ratingButton" id="p1" name="rate" value="p1">1</button>
                        <button type="submit" class="pTwo ratingButton" id="p2" name="rate" value="p2">2</button>
                        <button type="submit" class="pThree ratingButton" id="p3" name="rate" value="p3">3</button>
                    </div>
                </form>

            </div> <!--prop Box: End-->
            {% endfor %}

在我的python views.py中,我将其作为供稿使用:

@bp.route('/rate', methods=['POST','GET'])
def rate():
    if request.method == 'POST':
        note_list = ["m3", "m2", "m1", "z", "p1", "p2", "p3"]
        for i in note_list:
            titre = request.form["titre"]
            note = request.form['rate']
            if note == i:
                note = note_list.index(i)
                client.POSTS.rating.insert_one({"title" : titre, "rate" : note})
    return redirect(url_for('.proposer'))

但是,单击评分按钮时,我会收到此错误:

Traceback (most recent call last):
  File "/Library/Python/3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Library/Python/3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/gabriel/Dropbox/Evrisops/Evrisops_Server/views/routes.py", line 252, in rate
    titre = request.form["titre"]
  File "/Library/Python/3.7/site-packages/werkzeug/datastructures.py", line 431, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'titre'

无论如何,我认为问题可能仅来自“标题”,但我也不知道该如何解决...

顺便说一句,你们知道如何将值附加到json文档中的数组吗?使用python和mongodb运算符:/

非常感谢:)

2 个答案:

答案 0 :(得分:0)

我认为错误来自此行:

titre = request.form["titre"]

您可能在titre对象中没有request.form键。 为了调试此问题,我建议在循环中添加一个print request.form,以便您可以看到对象的结构。

此外,如果request.form["titre"]为空,则可能是因为您在value元素上设置了<p>属性,而HTML中是不允许的。

根据this page value属性无法在<p>元素上设置。尝试切换到<input>元素。

答案 1 :(得分:0)

好吧,前一段时间找到了解决方法 如果可以帮助任何人: 只需使用request.form.get('')而不是request.form[''] 以及带有隐藏属性的输入,以获取proposition.title的值。 :)