简体   繁体   English

为什么在Django中找不到URL 404?

[英]Why is the URL 404 not found with Django?

I have written a Django script that runs a Python parser to web s*e. 我已经编写了一个Django脚本,该脚本运行一个Python解析器到web s * e。 I am sending the request to the Django script via AJAX. 我正在通过AJAX将请求发送到Django脚本。 However, when the Ajax runs, it comes back as 404 not found for the URL. 但是,当Ajax运行时,它返回URL找不到404。 Why is this happening? 为什么会这样呢?

My code is below: 我的代码如下:

Ajax (with jQuery): Ajax(使用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: 我的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. 我的urls.py和我的views.py位于同一文件夹中,如果有任何区别的话。 They are just titled views.py and urls.py . 它们只是标题为views.pyurls.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. 另外,您还必须在JavaScript中添加斜杠。

I just resolved this: there was an error in my urls.py. 我刚刚解决了这个问题:urls.py中存在错误。 My system was having trouble with the .defaults that is was supposed to import from. 我的系统在应该从中导入的.defaults遇到问题。 Also, I didn't have a Django project set up, so It wouldn't import the views. 另外,我没有设置Django项目,因此它不会导入视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM