Django 根据浏览器的地理位置渲染视图

问题描述 投票:0回答:1

我试图根据浏览器给出的地理位置加载我的主页,如果坐标没问题,我加载这些坐标周围的机场列表,否则我加载所有机场。 但是,我无法在 GET 和 POST 请求之间进行管理。每次我都会获得页面中的所有机场。

事实上,当加载视图时(通过 GET 请求),所有机场都会显示,当页面完全加载后,将引发带有 GPS 信息的 POST 事件。因此执行了位置周围的机场列表,但渲染总是错误的。

非常感谢您的帮助

url.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.home_view),

views.py

def home_view(request):   

        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR') 

        if request.method == 'POST':
             Latitude = float(request.POST.get('latitude'))
             Longitude = float(request.POST.get('longitude'))
             context = aviationweatherapi.getStationInfo(Latitude,Longitude)
             print ("Get airports list around current GPS location")
             
        else:
            #The request is GET
            context = aviationweatherapi.getStationInfo(None, None)
            print ("Get all airports")
        
        context.update({'IP':ip})
        
        return render(request,'acceuil.html',context)

template_Base.html(acceuil.html 的基础)

<body>
    {% csrf_token %}
    {% block contenu %} {% endblock %}

    

    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
    
    <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
    
    <script>
        const findLoc = () => {
             const success = (position) => {
                const latitude = position.coords.latitude;
                const longitude = position.coords.longitude; 
                var data = {'latitude': latitude,
                            'longitude': longitude };
                data.csrfmiddlewaretoken = $('input[name=csrfmiddlewaretoken]').val();
                console.log('Location is on');
                console.log(data);
                document.getElementById("LocationLat").innerHTML = latitude;
                document.getElementById("LocationLon").innerHTML = longitude;
                $.post("/", data);
            }
    
            const error = () => {
                console.log('Location gathering Not allowed')
                const latitude = 0  // Default Latitude
                const longitude = 0    // Default Longtitude
                var data = {'latitude': latitude,
                            'longitude': longitude,
                        };
                data.csrfmiddlewaretoken = $('input[name=csrfmiddlewaretoken]').val();
                console.log(data);
                document.getElementById("LocationLat").innerHTML = "err.latitude";
                document.getElementById("LocationLon").innerHTML = "err.longitude";
                $.post("/", data);
            }
    
            navigator.geolocation.getCurrentPosition(success,error);
        }
        // Location is fetched when user loads screen
        Window.onload = findLoc();
      </script>

django ajax post view request
1个回答
0
投票

没有看到 navigator.geolocation.getCurrentPosition(success,error);很难看出到底发生了什么。但有一件事似乎很奇怪。在你的 get 请求之后,post 会被自动调用,但是在重新加载时,它会被 window.onload 一次又一次地调用,因为你的 base.html 被重新加载,并且脚本被再次调用。

© www.soinside.com 2019 - 2024. All rights reserved.