将控制台中的数据存储在数据库中并进行访问
Storing the data in the console in a database and accessing it
我正在构建一个网络应用程序,当为此按下特定按钮时访问用户的位置我正在使用 HTML geolocation api。
下面是 location.js 文件:
`var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
console.log(position.coords.latitude)
console.log(position.coords.longitude)
}
下面是 HTML 文件的片段:
<button onclick="getLocation()">HELP</button>
<p id="demo"></p>
<script src="../static/location.js"></script>
我想做的是将此信息(即用户的 longitude/latitude)发送到与该用户关联的电子邮件列表,但我不知道如何存储此数据并在之后访问此数据按钮被按下。
如果有人能让我开始了解如何保存与用户相对应的数据并从数据库访问它,那将非常有用。
如果您想将此信息存储到 Django 数据库,那么在 Django 视图中执行此操作可能更容易。这可能是一个 RedirectView,它在单击按钮后重定向到同一视图。
我以前使用过下载的 GeoLite2-City.mmdb 数据库,它可能并不总是最新的,但没关系。
你可以在 django 中使用 ipware 库获取请求的 ip 地址。然后在IPy中转换成pythonIP对象。然后,您可以使用 geoip 库从数据库中获取信息。
导入以下库;
from ipware.ip import get_ip
from IPy import IP
import geoip2.database
那么您获取 IP 的方法类似于
class MyRedirectView(RedirectView)
def get_redirect_url(self, request, *args, **kwargs):
## Write some code to handle the redirect url first ##
ip_address = get_ip(self.request)
"""Ensure that the IP address is a valid IP first"""
try:
IP(ip_address)
except Exception:
logger.exception("GEOIP2 error: ")
"""Then get the IP location"""
geo_path = settings.GEOIP_PATH
reader = geoip2.database.Reader(geo_path + '/GeoLite2-City.mmdb')
try:
response = reader.city(ip_address)
city = response.city.name
country = response.country.name
### Some code here to save to your DB
return super(MyRedirectView, self).get_redirect_url(*args, **kwargs)
如果您需要更准确的 IP 定位服务,您可以调用 API 调用类似 http://ip-api.com/ 的内容。但是在提供下一个视图之前,您必须等待此响应。
我正在构建一个网络应用程序,当为此按下特定按钮时访问用户的位置我正在使用 HTML geolocation api。
下面是 location.js 文件:
`var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
console.log(position.coords.latitude)
console.log(position.coords.longitude)
}
下面是 HTML 文件的片段:
<button onclick="getLocation()">HELP</button>
<p id="demo"></p>
<script src="../static/location.js"></script>
我想做的是将此信息(即用户的 longitude/latitude)发送到与该用户关联的电子邮件列表,但我不知道如何存储此数据并在之后访问此数据按钮被按下。 如果有人能让我开始了解如何保存与用户相对应的数据并从数据库访问它,那将非常有用。
如果您想将此信息存储到 Django 数据库,那么在 Django 视图中执行此操作可能更容易。这可能是一个 RedirectView,它在单击按钮后重定向到同一视图。
我以前使用过下载的 GeoLite2-City.mmdb 数据库,它可能并不总是最新的,但没关系。
你可以在 django 中使用 ipware 库获取请求的 ip 地址。然后在IPy中转换成pythonIP对象。然后,您可以使用 geoip 库从数据库中获取信息。
导入以下库;
from ipware.ip import get_ip
from IPy import IP
import geoip2.database
那么您获取 IP 的方法类似于
class MyRedirectView(RedirectView)
def get_redirect_url(self, request, *args, **kwargs):
## Write some code to handle the redirect url first ##
ip_address = get_ip(self.request)
"""Ensure that the IP address is a valid IP first"""
try:
IP(ip_address)
except Exception:
logger.exception("GEOIP2 error: ")
"""Then get the IP location"""
geo_path = settings.GEOIP_PATH
reader = geoip2.database.Reader(geo_path + '/GeoLite2-City.mmdb')
try:
response = reader.city(ip_address)
city = response.city.name
country = response.country.name
### Some code here to save to your DB
return super(MyRedirectView, self).get_redirect_url(*args, **kwargs)
如果您需要更准确的 IP 定位服务,您可以调用 API 调用类似 http://ip-api.com/ 的内容。但是在提供下一个视图之前,您必须等待此响应。