python - 在我的 Flask 应用程序中询问用户他们的位置并在他们离开网站时摆脱它
问题描述
我如何向用户询问他们的位置,将该位置用于某些获取请求,并在他们离开网站后删除该位置。我正在设计一个学生可以与学校匹配的系统。我有帖子形式的学校,我需要按与当前用户的距离来订购每所学校的帖子。但由于安全漏洞问题,我不允许将该位置实际存储在我的主 SQL 数据库中。我只能在学生离开网站之前使用它。关于我如何做到这一点的任何想法?这是我的主要家庭路线代码:
@main.route("/", methods=['GET', 'POST'])
@main.route("/home", methods=['GET', 'POST'])
def home():
if current_user.is_anonymous:
return render_template('home_ams.html', title='Home')
page = request.args.get('page', 1, type=int)
posts_dis = Post.query.all()
if current_user.is_authenticated:
for post in posts_dis:
api_key = "myapikey"
url1 = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
url2 = "&destinations="
url3 = "&mode=car&key="
origin = current_user.location
school = post.school_location_p
origin_up = origin.replace(' ', '+')
school_up = school.replace(' ', '+')
url_full = url1+origin_up+url2+school_up+url3+api_key
output = requests.get(url_full).json()
for obj in output['rows']:
for data in obj['elements']:
x = data['distance']['value']
y = x /1000
post.distance = round(y)
posts = Post.query.order_by(Post.distance.asc()).paginate(page=page, per_page=5)
if post.distance != "X":
posts = Post.query.order_by(Post.distance.asc()).paginate(page=page, per_page=5)
else:
posts = Post.query.order_by(Post.school_title.desc()).paginate(page=page,
per_page=5)
return render_template('home.html', posts=posts)
解决方案
推荐阅读
- node.js - 节点 - 测试字符串是否包含数组元素用相同数组的随机元素替换他(同义词)
- elasticsearch - 如何在 ElasticSearch 加载管道中解码 JSON
- python - 我正在尝试使用拍摄方法找到一个周期轨道
- sorting - 谷歌工作表自动排序脚本修改仅在指定列更改时触发
- python - 图标桌面通过单击启动 python 脚本(Ubuntu)问题
- java - 使用 Swagger 1.5 使用 Map 属性配置 Pojo 不会生成“additionalProperties”
- flutter - 在文本中检测 @ 并将其显示为链接 (Flutter)
- r - 在 R 中加入时间序列
- python - 从没有浏览器的网站中提取数据(python)
- sql - 从 oracle 12.1 查询具有“。”字段值的 json 数据