0

I am building a BlogApp and I am stuck on a Problem.

What i am trying to do

I am trying to access user location like :- City or State. I don't want to access in Map. I just want to access the city name of users.

What have i tried

  • I try through JavaScript and Google Apis to access location , It showed the location perfectly. BUT i didn't find any way to save the location of User in Database from FrontEnd to BackEnd. So i dropped down that method. The Below ↓ code is showing the FrontEnd Code of JavaScript.

     <script>
    
    let thisElement = $(this)
    $.ajax({
      url: "https://geolocation-db.com/jsonp","/update_counter/",
      jsonpCallback: "callback",
      dataType: "jsonp",
      success: function(location) {
        $('#country').html(location.country_name);
        console.log('success')
        thisElement.parent().parent().fadeOut();
      }
    });
    
    </script>
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    
    <div>Country: <span id="country"></span></div>
    
  • Then i tried MaxMind's GeoIP it also didn't work for me. ( Also answers are too Old ).

  • Then i tried THIS BUT they are not connected to any model, so it may be difficult to save user location in DataBase.

I don't know what to do.

Any help would be appreciated.

Thank You in Advance.

Lars
  • 1,234
  • 1
  • 8
  • 31
  • I suppose, you can try following: After your ajax with user geo data is done you can try to initiate new ajax (for example: first ajax change element - write user city on page ) for changing element. New ajax should write data to db. – koko Mar 14 '21 at 06:30
  • I never used `ajax` and i also don't know how to save data in `db`. – Lars Mar 14 '21 at 06:31
  • I am studying too. I will write my thoughts as answer. – koko Mar 14 '21 at 06:34
  • Okay Thanks. Always Welcome – Lars Mar 14 '21 at 06:35

2 Answers2

1
  1. I think you cant start new ajax as success after first one. In this case after first ajax is done you should write City on the page element.
  2. After you got element with City on page, you can try to initiate new ajax for changing element
$( "<your element with City name>" ).change(function() {
      const city = <something where you wrote your City>;
        $.ajax ({
        url : <your url>,
        data: {
                'city': city,
            },
        success: function (data) {
                <do something if you want>;
            }
        });
    });
  1. Django: you can read this for understanding ajax (I just googled it). In simple words, you initiate ajax to your server(you create url point for it), for that url you create function to save data to db. (Sorry for my english)

url:

...
path('<your url>', views.your_function, name='ajax-something'),
...

views:

def your_function(request):
     city = request.GET.get('city')
     <here you write data to db>

     return <what you want to do>
koko
  • 397
  • 2
  • 11
  • When i change `datatype` to `data` to store city then the city is not showing. – Lars Mar 14 '21 at 07:30
  • If you meant `datatype` in your first ajax: you should't combine two ajax, they do different work. First - get data from geo api and put City to page if success, that's it. Second take data from element (when it was changed via success of first ajax) where City and send data to Django. Its different requests. – koko Mar 14 '21 at 07:58
  • You mean , I should make a new `ajax` right after old `ajax` ? – Lars Mar 14 '21 at 08:01
  • AND also , Can i ask, What is `element_with_city_name` in `ajax` ? – Lars Mar 14 '21 at 08:02
  • Yes, my view is: you should have two different ajax'es. First take data from geo api and put it to 'element_with_city_name'. Second take data from 'element_with_city_name' and send it to Django. – koko Mar 14 '21 at 08:07
  • AND where to put the name of the `Model instance` ? – Lars Mar 14 '21 at 09:16
  • In my example in `def your_function` I showed how to get data and did comment `` this place for your logic. – koko Mar 14 '21 at 12:06
0

Use Ajax.

After getting your location. Use Ajax to send it to django and them use django to save to db.

Chymdy
  • 662
  • 4
  • 14