用python写一个前端在企业产品页面的输入产品名称,产品类型,市场定位进行查询企业产品详细信息;并可以进行收藏,评论等操作
时间: 2023-05-11 10:05:45 浏览: 51
可以使用Python的Web框架,如Django或Flask,来实现这个前端功能。首先,需要创建一个HTML表单,让用户输入产品名称、产品类型和市场定位。然后,将这些信息发送到后端,使用SQLAlchemy等ORM工具查询数据库中的企业产品详细信息。在查询结果页面上,可以添加收藏和评论功能,使用AJAX技术实现异步请求和响应。以下是一个简单的示例代码:
HTML模板:
```html
<form method="POST" action="/search">
<label for="name">产品名称:</label>
<input type="text" name="name" id="name">
<br>
<label for="type">产品类型:</label>
<input type="text" name="type" id="type">
<br>
<label for="location">市场定位:</label>
<input type="text" name="location" id="location">
<br>
<button type="submit">查询</button>
</form>
<div id="result"></div>
<script>
$(document).ready(function() {
$('form').submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: '/search',
data: $('form').serialize(),
success: function(response) {
$('#result').html(response);
}
});
});
});
</script>
```
Python后端代码:
```python
from flask import Flask, request, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///products.db'
db = SQLAlchemy(app)
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
type = db.Column(db.String(50), nullable=False)
location = db.Column(db.String(50), nullable=False)
details = db.Column(db.Text, nullable=False)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search', methods=['POST'])
def search():
name = request.form['name']
type = request.form['type']
location = request.form['location']
products = Product.query.filter_by(name=name, type=type, location=location).all()
return render_template('result.html', products=products)
@app.route('/product/<int:id>', methods=['GET', 'POST'])
def product(id):
product = Product.query.get(id)
if request.method == 'POST':
comment = request.form['comment']
product.comments.append(comment)
db.session.commit()
return render_template('product.html', product=product)
if __name__ == '__main__':
app.run(debug=True)
```
其中,`Product`是一个ORM模型,对应数据库中的`products`表。`search`函数接收前端发送的POST请求,从数据库中查询符合条件的产品信息,并将结果渲染到`result.html`模板中。`product`函数接收前端发送的GET或POST请求,显示产品详细信息和评论,并将新评论添加到数据库中。