简体   繁体   中英

Why is the URL 404 not found with Django?

I have written a Django script that runs a Python parser to web s*e. I am sending the request to the Django script via AJAX. However, when the Ajax runs, it comes back as 404 not found for the URL. Why is this happening?

My code is below:

Ajax (with jQuery):

//send a `post` request up to AWS, and then 
//insert the data into the paths

$.post('/ca', function(data){
//evaluate the JSON
data = eval ("(" + data + ")");
//insert the vars into the DOM
var contentOne;
contentOne = data.bridge_time;
contentOne += 'min delay';
$('#timeone').html(contentOne);
var contentTwo;
contentTwo = data.tunnel_time;
contentTwo += 'min delay';
$('#timetwo').html(contentTwo);
//if this falls through, push an error.
var tunnel_time = data.tunnel_time;
var bridge_time = data.bridge_time;
var tunnel = document.getElementById('tunnel');
var bridge = document.getElementById('bridge');
var tunnelText = document.getElementById('timeone');
var bridgeText = document.getElementById('timetwo');
//algo for the changing icons. Kudos to Vito
if(tunnel_time<bridge_time){
  tunnel.src="tunnel3.png";
  bridge.src="bridge2r.png";
}else if( bridge_time<tunnel_time){
  bridge.src="bridge21.png";
  tunnel.src="tunnel2r.png";
}else{
  bridge.src="bridge2n.png";
  tunnel.src="tunnel2g.png";
}
  $.fail(function() {
alert("We're sorry. We are having an error. Check back later.");
});
});

My urls.py:

from django.conf.urls.defaults import *
from views import views

urlpatterns = patterns('',
                   (r'^/us', views.american_time),
                   (r'^/ca', views.canadian_time),
)

My urls.py and my views.py are in the same folder, if that makes any difference. They are just titled views.py and urls.py . Thank you!

Try

from django.conf.urls.defaults import *
from views import views

urlpatterns = patterns('',
     (r'^/us/$', views.american_time),
     (r'^/ca/$', views.canadian_time),
)

Also you have to add the trailing slash in your JavaScript.

I just resolved this: there was an error in my urls.py. My system was having trouble with the .defaults that is was supposed to import from. Also, I didn't have a Django project set up, so It wouldn't import the views.

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