【问题标题】:Saving Longitude and Latitude to a user profile in Django将经度和纬度保存到 Django 中的用户配置文件
【发布时间】:2021-07-05 02:02:42
【问题描述】:

我正在寻找一种使用 HTML5(可能还有 JS)将访问者/用户经纬度数据保存到数据库的方法。我不希望在外面使用包,因为它们似乎有点过时,并且考虑到它们自己对其他 API 的依赖,将来可能会破坏我的代码。

我知道有一种使用 AJAX 的方法,但我显然不了解和理解它以实现它。

我对学识渊博的领主的要求是 - 1. 获取 Loc 数据 2. 以 dict 或 json 或字符串格式将其发送到 Python,从中可以进一步处理和保存。

您为什么会问 - 好问题。我会用它来显示主页上的天气和“登录”页面上的本地推特趋势。

任何帮助将不胜感激。

干杯!

我的 JS 代码如下:

    // Set up global variable
var result;

function showPosition() {
    // Store the element where the page displays the result
    result = document.getElementById("result");

    // If geolocation is available, try to get the visitor's position
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
        result.innerHTML = "Getting the position information...";
    } else {
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
};

// Define callback function for successful attempt
function successCallback(position) {
    result.innerHTML = [position.coords.latitude, position.coords.longitude];
}

// Define callback function for failed attempt
function errorCallback(error) {
    if (error.code == 1) {
        result.innerHTML = "You've decided not to share your position, but it's OK. We won't ask you again.";
    } else if (error.code == 2) {
        result.innerHTML = "The network is down or the positioning service can't be reached.";
    } else if (error.code == 3) {
        result.innerHTML = "The attempt timed out before it could get the location data.";
    } else {
        result.innerHTML = "Geolocation failed due to unknown error.";
    }
}

window.onload = showPosition;

【问题讨论】:

  • 这是否满足您的需求? MDN | Geolocation API
  • 在您的前端,您可以使用 JS 调用 api 并使用位置更新用户配置文件。 w3schools.com/html/html5_geolocation.asp
  • @shadow-lad:感谢您的链接,但我已经有了纬度和经度...我是新手,因此不知道如何附加屏幕截图。如前所述。我只是在寻找一种将 JS 值传输到 Python 以进行分析和 Twitter 趋势的解决方案。
  • @LinhNguyen 在前端一切工作正常......我有那里需要的东西......我需要将它发送回 Django / Python 进行分析。非常感谢。
  • 这可能对你有帮助:stackoverflow.com/questions/18978152/…

标签: python html django location geo


【解决方案1】:

什么对我有用:

首先我建议你在你的模型上使用models.PointField

当我在 FE 上获取 long/lat 数据时,我将其作为 form-data 以以下格式发送,例如:

"{\"type\":\"Point\",\"coordinates\":[14.215641,50.0100000001]}"

然后我将它映射到模型字段并保存。它保存得很好,以后我可以用它查询谷歌地理编码器或任何东西。

【讨论】:

  • 我的错,我忘记了 Pointfield 来自 GeoDjango,你不想使用包。在这种情况下,我建议以@shaedrich 的答案中描述的形式发送数据,然后将其映射保存到模型上的两个models.Floatfield(s)。
【解决方案2】:

在你的 JS 代码中:

function successCallback(position) {
    result.innerHTML = [position.coords.latitude, position.coords.longitude];
    $.post('your-python-endpoint-url', {
        latitude: position.coords.latitude,
        longitude: position.coords.longitude
    });
}

在你的蟒蛇中:

def index(request):
    if request.is_ajax():
        if request.method == 'POST':
            print 'Raw Data: "%s"' % request.body   
    return HttpResponse("OK")

根据您的需要更改方法名称和主体,不要忘记在 django 中定义路由。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多