I have scripts one for a flask app and another for a barcode scanner script that sends post requests.
The flask app receieves it and decodes the JSON to the data I want to display on my html.
But how do I load these into the html? Here are the logs when the flask app receives the request:
Fissan Foot Powder
80.5
127.0.0.1 - - [19/Apr/2019 14:09:10] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [19/Apr/2019 14:09:19] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [19/Apr/2019 14:09:21] "GET /static/assets/img/favicon.png HTTP/1.1" 200 -
and here's my code:
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
try:
r = request.data
data = r.decode('utf-8')
data = json.loads(data)
item = data['item']
price = data['price']
print(item)
print(price)
return render_template('dashboard.html', item=str(item), value=str(price))
except Exception as e:
print(str(e))
item = 'Test'
price = 'Test'
return render_template('dashboard.html', item=str(item), value=str(price))
What should be done in order for item and price to display whenever a POST request is sent to the flask app? As of now nothing is displayed on the html.
What I tried next after a suggestion:
# Index route
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
try:
r = request.data
data = r.decode('utf-8')
return redirect(url_for('item', data=data))
except Exception as e:
print(str(e))
item = 'Test'
price = 'Test'
return render_template('dashboard.html', item=str(item), value=str(price))
# Item route
@app.route("/item/", methods=['GET', 'POST'])
def item():
data = request.args['data']
print(str(data))
data = json.loads(data)
item = data['item']
price = data['price']
print(item)
print(price)
return render_template('dashboard.html', item=item, value=price)
In the log it says that the request is succesful with:
127.0.0.1 - - [19/Apr/2019 16:35:08] "POST / HTTP/1.1" 302 -
{"item": "Fissan Foot Powder", "price": "80.5"}
Fissan Foot Powder
80.5
127.0.0.1 - - [19/Apr/2019 16:35:08] "GET /item/?data=%7B%22item%22%3A+%22Fissan+Foot+Powder%22%2C+%22price%22%3A+%2280.5%22%7D HTTP/1.1" 200 -
However when I look at the interface it remains the same. It didnt redirect to the url where it should go to.
also about the html I already put {{ item }}
and {{ value }}
in the places I want the scanned item to show to.
What I wanted to achieve is that when the request is triggered, this passes to the /item/
route where it recieves the payload and by the GET request it should have rendered the template together with the payloads item
and price
as jinja on the html. And should have shown there. What should I do to achieve this?