要实现点击收藏显示收藏次数的功能,需要先在页面中渲染一个收藏按钮,并且与后端通信获取收藏次数数据。以下是一个简单的实现步骤:

1. 在页面中添加一个收藏按钮,并设置一个点击事件:
```html
```
2. 使用JavaScript通过Ajax请求后端接口获取收藏次数数据,并更新页面显示:
```javascript
document.getElementById('favoriteButton').addEventListener('click', function() {
// 模拟Ajax请求获取收藏次数数据
fetch('https://example.com/favorite_count')
.then(response => response.json())
.then(data => {
document.getElementById('favoriteCount').innerText = data.count;
});
});
```
3. 在后端设置一个接口用于获取收藏次数数据,返回一个 JSON 格式的数据:
```python
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/favorite_count')
def get_favorite_count():
count = 100 # 假设收藏次数为100
return jsonify({'count': count})
if __name__ == '__main__':
app.run()
```
通过以上步骤,点击收藏按钮时会触发Ajax请求后端接口获取收藏次数数据,并在页面上显示收藏次数。