0

I really need some help on this subject, which seems easy, but i cant get myself to figure out the problem. I understand that using ajax is the best way to pass a variable from a template to a view. Is what i have done good, or would you recommend i try something different ?

 function updateArea(e) {
var data = draw.getAll();
var answer = document.getElementById('calculated-area');
if (data.features.length > 0) {
var area = turf.area(data);
// restrict to area to 2 decimal points
var rounded_area = Math.round(area*100)/100;
answer.innerHTML = '<p><strong>' + rounded_area + '</strong></p><p>square meters</p>';
var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));
document.getElementById('export').setAttribute('href', 'data:' + convertedData);
document.getElementById('export').setAttribute('download','data.geojson');
} else {
answer.innerHTML = '';
if (e.type !== 'draw.delete') alert("Use the draw tools to draw a polygon!");

}
}

I want to take the area variable and convertedData variable and pass them to a view.

So after some searching and trial and error, here is what I tried to do using Ajax. When I run this i get no errors, but i get nothing in my database. template.html

<form id="areaform" method="POST">
          {% csrf_token %}
          <input type="submit" name="area" value=""/>
        </form>
$(document).on('submit', '#areaform',function(e) {
          e.preventDefault();    
          $.ajax({
            type: 'POST',
            url:'fertisatmap/area',
            data:{
                name: $('#area').val(),
            },
            sucess:function(){}    
          });
    });

urls.py

path('fertisatmap/area', fertisat_views.fertisatareaview, name='fertisat-area'),

views.py

def fertisatareaview(request):
    if request.method == "POST":
        area = request.POST['area']
        Fertidb.objects.create(
            area = area)  
        return HttpResponse('')
Benda
  • 31
  • 3
  • Does it matter that you spelled `sucess:function()` with only one `c` in `success`? – jcfollower Oct 07 '19 at 22:02
  • Doesn't solve the problem – Benda Oct 08 '19 at 15:57
  • Does this help? https://stackoverflow.com/questions/13465711/how-do-i-post-with-jquery-ajax-in-django Shouldn't your data: name: be '#areaform'? Does your form have any values in it? – jcfollower Oct 08 '19 at 21:04
  • Thanks @jcfollower, I want the data name to be area which is a variable i have in a function in the script – Benda Oct 08 '19 at 21:21

0 Answers0