[Django]-Django page not found, ajax request not working correctly

4đź‘Ť

âś…

Remove the trailing slash before the $ in your url regex or use a ? before the $.

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^chart_data$', views.chart_data, name='chart_data')
]

You can also refer it by name in templates as {% url 'app:url_name' %}
You’ll also need to pass csrf token along with your post data.

{% csrf_token %}
<script>
    var timeArray = [0,10,20,30,40,50,60];
    var dataArray = [0,0,0,0,0,0,0];

    $.ajax({
        var csrftoken = $('[name="csrfmiddlewaretoken"]').val();
        url: {% url 'headstation:chart_data' %},
        type:"POST",
        data: {
            'data': 'temperature',
            'csrfmiddlewaretoken': csrftoken,
        },
        dataType: 'json',
        success: function (data) {
            if (data) {
                timeArray = data.x;
                dataArray = data.y;
                alert("recieved");
            }
        }
    });
</script>
👤MohitC

0đź‘Ť

You can use ” (blank space) instead ^$. It’s working for me.

url('^$', views.index, name='index'),
url('about/', views.about, name='about'),

Leave a comment