简体   繁体   中英

Is it possible to use JavaScript generated text from Django templates using the“with” templatetag

I am using the following W3schools JavaScript code to generate latitude and longitude results in my Django templates:

I was wondering if it was possible to use The results spat by the JavaScript to my django variables using the {% with ... %} template tag

Below is my user.profile model

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)        
    age = models.DateField(blank=True, null=True)
    member_since = models.DateTimeField(auto_now_add=True)
    profile_image = models.ImageField(default='', blank=True, null=True)
    lat = models.FloatField(blank=True, null=True)
    lon = models.FloatField(blank=True, null=True)
    point = models.PointField(srid=4326, default='SRID=4326;POINT(0.0 0.0)')
    objects = models.GeoManager()

You need to create a function in which AJAX call goes to one of your URL.
In AJAX call you need to send lat and lon values and save them to Database.
Call this AJAX function on document/page load.

You can either make a POST or a GET call using AJAX.

var element = $("#element-id")    

$.ajax({
url : $(element).attr("data-url"),
data : {
    "csrfmiddlewaretoken" : $(element).siblings("input[name='csrfmiddlewaretoken']" ).val(),
    "alt":lat,
    "lon": lon
},
method: "POST",
dataType : "json",
success : function (returned_data) {            
    // display data 
}});

If you are sending a GET call, ignore the csrf token.

Where element is hidden field/link/button in HTML with data-url attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM