英文:
Python & Flask, send and receive data from server
问题
I am trying to do a practice, which consists of sending some data from an html form, receive it, execute a function and return it to the html.
From the index.html file, I have declared a form with the <form>
label.
With the action attribute, I pass to the URL /products/mouse the name of a product, which is defined in a dictionary inside a file with the name products.py.
This value is collected by the index()
function, calls the getProduct()
function and passes it the variable product_nombre
which contains the value mouse sent from index.html.
getProduct
, goes through the products dictionary looking for a product that matches the name of the product sent by html, in this case, mouse.
The question is that in the index.html, it indicates to me:
Producto no encontrado - Product not found when the product exists
Why does this happen?
this is the content of my main app.py file:
from flask import Flask, jsonify, request, render_template, redirect, url_for
from products import products
app = Flask(__name__)
@app.route('/')
def index():
product_nombre = request.args.get('product_nombre', 'punto control')
data = getProduct(product_nombre)
return render_template('index.html', data=data)
@app.route('/products/<string:product_nombre>', methods=['GET'])
def getProduct(product_nombre):
for product in products:
if product['nombre'] == product_nombre:
return product
return "Producto no encontrado"
if __name__ == "__main__":
app.run(debug=True)
this is the contents of my index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/products/mouse" method="get">
<h2 id>Hola mundo</h2>
<h4> {{ data }}</h4>
</form>
</body>
</html>
this is the content of the file products.py
products = [
{"nombre": "laptop",
"precio": 800,
"cantidad": 4},
{"nombre": "mouse",
"precio": 40,
"cantidad": 10},
{"nombre": "monitor",
"precio": 250,
"cantidad": 3}
]
英文:
I am trying to do a practice, which consists of sending some data from an html form, receive it, execute a function and return it to the html.
From the index.html file, I have declared a form with the <form>
label.
With the action attribute, I pass to the URL /products/mouse the name of a product, which is defined in a dictionary inside a file with the name products.py.
This value is collected by the index()
function, calls the getProduct()
function and passes it the variable product_nombre
which contains the value mouse sent from index.html.
getProduct
, goes through the products dictionary looking for a product that matches the name of the product sent by html, in this case, mouse.
The question is that in the index.html, it indicates to me:
>Producto no encontrado - Product not found when the product exists
Why does this happen?
this is the content of my main app.py file:
from flask import Flask, jsonify, request, render_template, redirect, url_for
from products import products
app = Flask(__name__)
@app.route('/')
def index():
product_nombre = request.args.get('product_nombre', 'punto control')
data = getProduct(product_nombre)
return render_template('index.html', data=data)
@app.route('/products/<string:product_nombre>', methods=['GET'])
def getProduct(product_nombre):
for product in products:
if product['nombre'] == product_nombre:
return product
return "Producto no encontrado"
if __name__ == "__main__":
app.run(debug=True)
this is the contents of my index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/products/mouse" method="get">
<h2 id=>Hola mundo</h2>
<h4> {{ data }}</h4>
</form>
</body>
</html>
this is the content of the file products.py
products = [
{"nombre": "laptop",
"precio": 800,
"cantidad": 4},
{"nombre": "mouse",
"precio": 40,
"cantidad": 10},
{"nombre": "monitor",
"precio": 250,
"cantidad": 3}
]
答案1
得分: 0
在index.html中,它对我表示:
Producto no encontrado
我理解这是发生在
@app.route('/')
,对吗?那么,在那里的流程是什么?product_nombre = request.args.get('product_nombre', 'punto control')
这获取了一个 请求参数,即URL查询参数。如果只是访问
localhost:5000/
(或其他什么),那么就没有参数。所以product_nombre
将获取默认值'punto control'
。嗯,然后getProduct(product_nombre)
当然不会匹配任何东西,因为没有产品叫做 "punto control",所以你会得到默认的返回值 "Producto no encontrado"。
英文:
> in the index.html, it indicates to me:
>
> > Producto no encontrado
I take this to mean that it happens in @app.route('/')
, right? Well, then what's the flow there?
> product_nombre = request.args.get('product_nombre', 'punto control')
This gets a request argument, i.e. URL query parameters. If you just visit localhost:5000/
(or whatever), then there are no arguments. So product_nombre
will get the default value of 'punto control'
. Well, then getProduct(product_nombre)
of course doesn't match anything, because there's no product "punto control", so you get the default return "Producto no encontrado"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论