'Calling Django Function from JS Ajax not working
I am trying to call a python function that calculates and converts some number input from a user form.
I have an onclick button event to call the following JS function
$.ajax({
type: "POST",
url:"/calc_ha_range/",
dataType: 'json',
data: {'ra_deg' : ra_deg, 'dec_deg': dec_deg, 'glon_deg': glon_deg, 'glat_deg': glat_deg},
success: function(data) {
jsonData = JSON.parse(data);
ha_start.setAttribute('min', jsonData.min);
ha_start.setAttribute('max', jsonData.max);
ha_end.setAttribute('min', jsonData.min);
ha_end.setAttribute('max', jsonData.max); // data.min = minimum val for HA range
}
})
I then have a django url mapping in the form of
import home.views, sensitivity_radec_astro.views,sensitivity_radec_astro_show.views, sensitivity_map.views, sensitivity_map_show.views, sensitivity_radec_vs_freq.views, sensitivity_radec_vs_freq_show.views, sensitivity_radec_vs_lst.views, sensitivity_radec_vs_lst_show.views, sensitivity_vs_freq.views, sensitivity_vs_freq_show.views, sensitivity_vs_lst.views, sensitivity_vs_lst_show.views, utils.sensitivity_db
urlpatterns = [
path('', home.views.load),
path('sensitivity_radec_astro/', sensitivity_radec_astro.views.load),
path('sensitivity_radec_astro_show/', sensitivity_radec_astro_show.views.sensitivity_radec_astro_show),
path('sensitivity_map/', sensitivity_map.views.load),
path('sensitivity_map_show/', sensitivity_map_show.views.sensitivity_map_show),
path('sensitivity_radec_vs_freq/', sensitivity_radec_vs_freq.views.load),
path('sensitivity_radec_vs_freq_show/', sensitivity_radec_vs_freq_show.views.sensitivity_radec_vs_freq_show),
path('sensitivity_radec_vs_lst/', sensitivity_radec_vs_lst.views.load),
path('sensitivity_radec_vs_lst_show/', sensitivity_radec_vs_lst_show.views.sensitivity_radec_vs_lst_show),
path('sensitivity_vs_freq/', sensitivity_vs_freq.views.load),
path('sensitivity_vs_freq_show/', sensitivity_vs_freq_show.views.sensitivity_vs_freq_show),
path('sensitivity_vs_lst/', sensitivity_vs_lst.views.load),
path('sensitivity_vs_lst_show/', sensitivity_vs_lst_show.views.sensitivity_vs_lst_show),
path('calc_ha_range/', utils.sensitivity_db.calc_hour_angle_range)
]
which refers to the following python function
def calc_hour_angle_range( request ):
geo_lat = EarthLocation.from_geodetic(lon="XXXX",lat="XXXX",height=XXXX).lat.value
if request.method == "POST":
ra_deg, dec_deg, glon_deg, glat_deg = request.POST["ra_deg"], request.POST["dec_deg"], request.POST["glon_deg"], request.POST["glat_deg"]
tan_geo_lat = math.tan( geo_lat*(math.pi/180.00) )
tan_dec = math.tan( dec_deg*(math.pi/180.00) )
cos_ha = -tan_dec * tan_geo_lat
ha_rad = math.acos( cos_ha )
ha_deg = ha_rad*(180.00/math.pi)
ha_h = ha_deg/15.00
output = {'min': -math.fabs(ha_h), 'max': +math.fabs(ha_h)}
My issue is when the ajax call takes place in the onClick function I get an error in the console saying 404: mysite.com/calc_ha_range/ not found
I have taken over this project from another amateur developer so my understanding of ajax and django is quite fragmented
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
