python - 如何将坐标从 javascript 文件发送到烧瓶服务器以放入数据库?
问题描述
我有一个简单的烧瓶应用程序,它接受用户通过文本区域编写的评论,并将其发布在我的数据库“评论”的“内容”列下的数据库中:
<div class="row">
<form action="." method="POST">
<textarea name="contents" placeholder="Enter a comment" class="form-control"></textarea>
<input type="submit" class="btn btn-success" value="Post comment">
</form>
</div>
然后在服务器上:
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return render_template("bulletin.html", comments=Comment.query.filter(Comment.content != ''))
comment = Comment(content=request.form["contents"])
db.session.add(comment)
db.session.commit()
return redirect(url_for('index'))
假设我在javascript中有两个变量,纬度和经度。如何将这些变量发送到我的烧瓶服务器以存储在同一个数据库表中?(假设我在表中创建了纬度和经度列)
谢谢
解决方案
首先设置 python 来接受这些信息。为要发布到的坐标创建端点
@app.route("/coords/<x>/<y>", methods=["POST"])
def coords(x, y):
# Have the code here to save to which ever database you use
然后让 javascript 向该 URL 发送一个发布请求。在您的示例中,您使用表单,因此使用 HTML 操作标签发布到特定 URL
推荐阅读
- python - 使用 pandas 将数据框中的值替换为字典中的值
- docker - 在 Azure 应用服务中,我应该使用单独的容器运行一个大的 Dockerfile 还是 docker-compose.yml?
- python - Python中的高阶函数
- java - 如何安全地从列表中删除元素
- python - 刷新标准输出时打印子进程索引
- angular - 中型编辑器,工具栏在选择文本时不显示
- flutter - 当不关注 Flutter 中的字段时,如何使图标在文本字段中可见?
- laravel-8 - 基于角色的多重身份验证 VS 多重保护身份验证
- python - 如何优化此代码,因为此代码通过 Time limit exceeded ,不同测试用例的运行时错误?
- wechat - 如何为微信小程序制作联系表