当用户使用地理位置网站时,是否可以将javascript html5地理位置纬度和经度保存到django管理员。web页面的目标是保存用户的经度和纬度值,以便以后当用户再次登录时可以访问数据。在
几年前,我在stackoverflow上发现了一个类似的问题,但一直没有得到任何答案。链接是:Save JavaScript GeoLocation data to Django admin page
如果有一个基于这个代码链接的答案,那就太好了。在
我读到的另一个选项是创建一个html表单,并将表单设置为jQuery根据javascript html5地理位置生成的数据自动填充表单。对我这样的初学者来说,这又是相当复杂的。在
无论是通过代码、教程、博客文章、示例还是链接,我都将非常感谢您的帮助。我不希望提供所有的编程代码(虽然我从示例中学到了更好的东西),但是如果有一些材料/示例可以帮助我实现我的编程任务。谢谢您。在
我目前在这里与我的进展,但仍然无法张贴纬度和经度到django管理页面:
代码如下:
django项目的结构如下:
-ajax
- __pycache__
- migrations
- __pycache__
0001_initial.py
__init__.py
- static
- css
- bootstrap.css
- fonts
- js
- script.js
- templates
- ajax
- base.html
- index.html
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- urls.py
- views.py
-server
- __pycache__
- __init__.py
- settings.py
- urls.py
- views.py
- wsgi.py
-db.sqlite3
-manage.py
在索引.html在
^{pr2}$在脚本.js在
var pos;
var $demo;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
$demo.text("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
pos = position;
var { latitude, longitude } = pos.coords;
$demo.html(`Latitude: ${latitude}<br>Longitude: ${longitude}`);
$('#btn_submit').attr("disabled", null);
}
$(document).ready(function() {
$demo = $("#demo");
$('#btn_submit').on('click', function() {
var data = pos.coords;
data.csrfmiddlewaretoken = $('input[name=csrfmiddlewaretoken]').val();
$.post("/ajax/", data, function() {
alert("Saved Data!");
});
});
});
在基本.html在
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'ajax/css/bootstrap.css' %}"/>
</head>
<body>
{% csrf_token %}
<nav class="navbar navbar-default">
<div class="container-fluid">
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">Python - Django Simple Submit Form With Ajax</h3>
<hr style="border-top:1px dotted #000;"/>
{% block body %}
{% endblock %}
</div>
</body>
<script src = "{% static 'ajax/js/jquery-3.2.1.js' %}"></script>
<script src = "{% static 'ajax/js/script.js' %}"></script>
</html>
在模型.py在
from django.db import models
# Create your models here.
class Member(models.Model):
latitude = models.DecimalField(max_digits=19, decimal_places=16)
longitude = models.DecimalField(max_digits=19, decimal_places=16)
在视图.py(阿贾克斯)
from django.shortcuts import render, redirect
from .models import Member
def index(request):
return render(request, 'ajax/index.html')
def insert(request):
member = Member(latitude=request.POST['latitude'], longitude=request.POST['longitude'])
member.save()
return redirect('/')
在网址.py(阿贾克斯)
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name="index"),
url(r'^insert$', views.insert, name="insert")
]
在视图.py(服务器)
from django.shortcuts import redirect
def index_redirect(request):
return redirect('/ajax/')
在网址.py(服务器)
from django.conf.urls import url, include
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index_redirect, name="index_redirect"),
url(r'^ajax/', include("ajax.urls")),
url(r'^admin/', admin.site.urls),
]
它“发布”数据,但它不会出现在django管理中。我浏览了许多网站,寻找原因的答案,但仍然没有找到答案。再次感谢你的帮助。在
我已经使用jQuery和Ajax将经度和纬度数据提交到任何要在其中存储这些数据的模型。在
在你的模型.py公司名称:
为了你视图.py在
^{pr2}$现在我们有了视图,我们可以设置一个url端点来提交post请求
最后对于实际形式
对于最后一步,假设您使用了链接示例中的js代码,那么您可以将这些坐标值分配给变量,这些变量将与post请求一起提交,当用户单击按钮时,该post请求触发,这里的id是您要提交数据的表单的id,而e.PreventDefault是在发布数据时阻止页面重新加载。最后,django需要csrf令牌才能提交表单。在
相关问题 更多 >
编程相关推荐