首页 > 解决方案 > 如何从 html 模板中的 jinja2 循环请求.form

问题描述

所以我在我的网站上有一个提要,列出了最近提出的所有新“提案”。这些在我的 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 运算符:/

非常感谢 :)

标签: pythonflaskjinja2

解决方案


我认为错误来自这一行:

titre = request.form["titre"]

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

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

根据此页面value无法在<p>元素上设置属性。尝试切换到一个<input>元素。


推荐阅读