Reputation: 11
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. 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. 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 :
{% 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 :
@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 ? Using python and mongodb operator :/
Thanks alot :)
Upvotes: 1
Views: 2480
Reputation: 11
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.
:)
Upvotes: 0
Reputation: 1084
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.
To debug this, I suggest to add a print request.form
in the loop so you can see the structure of the object.
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.
According tho this page the value
attribute can't be set on a <p>
element. Try to switch to an <input>
element.
Upvotes: 1