0

Can someone please assist me with this issue? I am new to Django and I've been trying to create a web-based application which gets the user location and displays the shops around them from the tutorial I followed online. However, the initial values were hard-coded in the views.py in order to change the user's location. I've asked a question before in order to find the best solution to this, and the kind people advised to use AJAX. I've been trying to come up with a solution for few hours now and its driving me insane. I have never used AJAX before so I apologize for this.

This is what I have so far, no issues or errors when running the application, however, the values are not changing for user's location

Please see attached code

views.py

user_location = Point(0, 0, srid=4326)

def process_loc(request):

    lat = float(request.GET.get('lat'))
    lon = float(request.GET.get('lon'))
    global user_location
    user_location = Point(lat, lon, srid=4326)
    return render(request, 'shops/index.html', {'lat': lat, 'lon': lon})



class Home(generic.ListView):
    model = Shop
    context_object_name = 'shops'
    queryset = Shop.objects.annotate(distance=Distance('location',
    user_location)
    ).order_by('distance')[0:15]
    template_name = 'shops/index.html'

As it can be seen I'm declaring the user_location is located outside the function and then changing it inside the function, so it can be used in Home class

index.html

<head>
        <meta charset="utf-8">
        <title>Shops</title>
        <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>
</head>

<body>
<script>
    $(document).ready(function() {

                     var lat, lon;

                     lat = 100.347541;
                     lon = -87.321;

                        $.ajax({
                    type: "GET",
                    url: 'process_loc',
                    data:{
                    'lat': lat,
                    'lon': lon,
                            },
                dataType : "json",
                success: function(stores) {
                    // do whatever in here for success, errors
                }
                })

                    })
</script>
</body>

I am using this code for AJAX, at the moment I just hardcoded the values in HTML just to get it working first as I am desperate to find a solution for this.

urls.py

urlpatterns = [
                path("admin/", admin.site.urls),
                path("", views.Home.as_view()),
                path('', include('pwa.urls')),
                path("search/", views.SearchResultsView.as_view()),
                path('process_loc', views.process_loc, name='process_loc'),

            ]

Output when the server is ran

[06/Dec/2019 12:00:57] "GET / HTTP/1.1" 200 11312
[06/Dec/2019 12:00:57] "GET /process_loc?lat=100.347541;&lon=-87.321 HTTP/1.1" 200 6621
[06/Dec/2019 12:00:58] "GET /manifest.json HTTP/1.1" 200 342
[06/Dec/2019 12:00:59] "GET /serviceworker.js HTTP/1.1" 200 3228

I will appreciate any response on this and I apologise for the messy code, thank you.

4
  • looks like your ajax is working. Can you post your HTML? Commented Dec 6, 2019 at 12:23
  • Have you enabled Debug=True in your settings.py file ? Commented Dec 6, 2019 at 12:52
  • Thanks for the reply guys, @NalinDobhal what part would you like to see exactly? Commented Dec 6, 2019 at 13:06
  • @PrakashS yes the Dbug is set to true Commented Dec 6, 2019 at 13:06

1 Answer 1

1

i think values not change, because are hardcoded in the ajax request definition just here:

<script>
    $(document).ready(function() {

                     var lat, lon;

                     lat = 100.347541;
                     lon = -87.321;

                        $.ajax({
                    type: "GET",
                    url: 'process_loc',
                    data:{
                    'lat': lat,
                    'lon': lon,
                            },
                dataType : "json",
                success: function(stores) {
                    // do whatever in here for success, errors
                }
                })

                    })
</script>

then you could have a form with two inputs a take the inserted in there and put in the request

    $(document).ready(function() {
                     //here get values from inputs
                     var lat, lon;

                     lat = 100.347541; //set here
                     lon = -87.321; //set here

                        $.ajax({
                    type: "GET",
                    url: 'process_loc',
                    data:{
                    'lat': lat,
                    'lon': lon,
                            },
                dataType : "json",
                success: function(stores) {
                    // do whatever in here for success, errors
                }
                })

                    })
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for the reply, I intend to use the GeoLocation in HTML5 that loads the position once the page is loaded without having the user to click anything. w3schools.com/html/html5_geolocation.asp, I was looking at this example here, would it be possible to add this without a button?
yes, if you browser support Geolocation, just with the example you can, change the function showPosition, to it send the request with thi position obtained
Would you mind showing me the right way to get this working correctly please, at this stage I'm not thinking straight to get this done sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.