简体   繁体   English

Flask:如何在接收POST请求时自动呈现模板

[英]Flask: How to Automatically Render Template When Receiving POST Request

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. flask应用程序会接收它,并将JSON解码为我想在html上显示的数据。

But how do I load these into the html? 但是,如何将它们加载到html中? Here are the logs when the flask app receives the request: 以下是flask应用收到请求时的日志:

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? 每当将POST请求发送到flask应用程序时,为了显示商品和价格,应该怎么做? As of now nothing is displayed on the html. 到目前为止,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. 它没有重定向到它应该去的URL。

also about the html I already put {{ item }} and {{ value }} in the places I want the scanned item to show to. 关于html的信息,我已经将{{ item }}{{ value }}放在了我希望扫描的项目显示的位置。

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. 我想要实现的是,当触发请求时,它传递到/item/路由,在其中接收有效负载,通过GET请求,它应该已经将模板以及有效负载itemprice呈现为html上的jinja。 And should have shown there. 并且应该在那里显示。 What should I do to achieve this? 我应该怎么做才能做到这一点?

优良作法是从帖子返回重定向,并使用get的响应将结果显示给用户-请参阅Post / Redirect / Get

您可以在dashboard.html文件中使用以下内容:

<p>item: {{item}} price: {{value}}</p>

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

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