Reputation: 234
I am new to Django and I am just trying to figure it out and I did posted it in official Django google group but no answer. I am working with Google Map API in my Django template for GeoLocation and I intend to populate DB table dynamically after extracting information from JavaScript in my Django template. Following is the code:
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
I need to save longitude and latitude with GeoLocation Info about the location in human readable format in my model. My Model is
class Address(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)
latitude = models.CharField(max_length=10)
longitude = models.CharField(max_length=10)
address = models.CharField(max_length=100)
.....
My form is:
class UserAddressForm(forms.ModelForm):
class Meta:
model = Address
fields = ['latitude', 'longitude', 'address'}
I need to figure it out in my view how to do it. It will be great if someone can refer me to right direction because I am really having hard time to figure it out as newbie.
Upvotes: 1
Views: 331
Reputation: 333
def fx(request):
#template = loader.get_template('fx.html')
template = loader.get_template('fx.html')
fj = fxdata()
r0 = fj[0]
r1 = fj[1]
r2 = fj[2]
r3 = fj[3]
r4 = fj[4]
r5 = fj[5]
r6 = fj[6]
# context = {"Symbol": sym, "Time":time, "Ask":ask, "Bid":bid,"High":high,"low":low,"Close":close}
context = {"R0":r0, "R1":r1, "R2":r2, "R3":r3, "R4":r4, "R5":r5, "R6":r6}
for key, index in context.items():
a=context[key]
forex_instance = forex.objects.create(symbol=str(a[0]),datetime = str(a[1]), ask = float(a[2]), bid = float(a[3]), high = float(a[4]),low = float(a[5]), close=float(a[6]) )
html = template.render(context)
return HttpResponse(html)
In this view function fj = fxdata()
return a scrape data and stored in db
meanwhile i'm trying to display this data in my template so i used html = template.render(context)
the context dictionary i passed to template over HttpResponse
Then I made a Ajax Call on the same template with setInterval using div id.
SO its started refreshing the templates. It call view function every time it refreshes. The data also keep storing in DB.
Then I used Django rest-framework to export this data into JS
Upvotes: 1
Reputation: 333
i don't have enough reputation to comment that why i'm trying to answer what i understood from this, please don't mind
I faced the same issue when i was doing live Forex data streaming display. You can do one thing. What i did is reverse approach(i don't know exactly how to do this).
you will be able at access those data in js now.
You can make ajax call using set-interval function to make it refresh every interval. if you do this it will invoke your view function that every particular interval so it will be saved in db over model as well as displaying in template.
i hope it helps.
Upvotes: 1