Skip to content

Commit e5f6ac5

Browse files
committed
Use form to add XP from POST request
1 parent 40947fc commit e5f6ac5

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import math
6-
from flask import Flask, render_template, redirect, url_for
6+
from flask import Flask, render_template, redirect, url_for, request
77
from flask_sqlalchemy import SQLAlchemy
88

99
app = Flask(__name__)
@@ -52,13 +52,13 @@ def index():
5252
return render_template("index.html", user=user)
5353

5454

55-
@app.route("/add_xp/<float:amount>", methods=["POST"])
56-
def add_xp(amount):
55+
@app.route("/add_xp", methods=["POST"])
56+
def add_xp():
5757
"""
5858
Add XP (experience points) based on entered amount.
5959
"""
6060
user = User.query.first()
61-
user.add_xp(amount)
61+
user.add_xp(float(request.form["amount"]))
6262
db.session.commit()
6363
return redirect(url_for("index"))
6464

templates/index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ <h1>Flask User Level XP System</h1>
1111
Level {{ user.level }}<br />
1212
{{ user.xp }} / {{ user.xp_required }} XP<br />
1313
<progress value="{{ user.xp }}" max="{{ user.xp_required }}"></progress>
14-
<button
15-
onclick="location.href='{{ url_for('add_xp', user_id=user.id, amount=1) }}'"
14+
<form
15+
action="{{ url_for('add_xp', user_id=user.id, amount=1) }}"
16+
method="post"
1617
>
17-
Add XP
18-
</button>
18+
<button type="submit">Add XP</button>
19+
</form>
1920
</ul>
2021
</body>
2122
</html>

0 commit comments

Comments
 (0)