简体   繁体   English

如何在html模板中从jinja2循环中请求

[英]How to request.form from a jinja2 loop in an html template

so i have a feed on my website listing all the new "proposals" made recently. 所以我在我的网站上有一个提要,列出了最近提出的所有新“提案”。 These are listed in a jinja2 loop in my html template. 这些在我的html模板中的jinja2循环中列出。 The thing is, that i have rating buttons under each proposal, and i want to be able to get the rating the user clicked on in python and then send it to the database. 问题是,我在每个提案下都有评级按钮,我希望能够获得用户在python中点击的评级,然后将其发送到数据库。 But i don't really know how to do that as i'm still a beginner.. 但我真的不知道怎么做,因为我还是初学者..

So here is what i have in my html template : 所以这是我的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 %}

In my python views.py, i have this for my feed : 在我的python views.py中,我有这个为我的feed:

@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'))

But well, i get this error when clicking on a rating button : 但是,点击评级按钮时出现此错误:

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'

Anyways, i think the problem may only come from "title" but i have no idea how to fix that either... 无论如何,我认为问题可能只来自“头衔”,但我不知道如何解决这个问题......

BTW, do you guys know how to append a value to an array in a json document ? 顺便说一句,你们知道如何在json文档中为数组附加值吗? Using python and mongodb operator :/ 使用python和mongodb运算符:/

Thanks alot :) 非常感谢 :)

I think the error comes from this line : 我认为错误来自这一行:

titre = request.form["titre"]

You probably don't have a titre key in the request.form object. 您可能在request.form对象中没有titre键。 To debug this, I suggest to add a print request.form in the loop so you can see the structure of the object. 为了调试这个,我建议在循环中添加一个print request.form ,这样你就可以看到对象的结构。

Moreover, if request.form["titre"] is empty, it is probably because you are setting a value attribute on a <p> element which is not allowed in HTML. 此外,如果request.form["titre"]为空,则可能是因为您在<p>元素上设置了一个value ,该属性在HTML中是不允许的。

According tho this page the value attribute can't be set on a <p> element. 根据此页面 ,无法在<p>元素上设置value属性。 Try to switch to an <input> element. 尝试切换到<input>元素。

Okay, found the way to do that some time ago If it can help anyone : just use request.form.get('') instead of request.form[''] Along with an input with hidden property to get the value of the proposition.title. 好的,前段时间找到了这样做的方法如果它可以帮助任何人:只需使用request.form.get('')而不是request.form['']以及带有hidden属性的输入来获取值的proposition.title。 :) :)

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

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