首页 > 解决方案 > 在我的 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)

标签: pythonflask

解决方案


推荐阅读