为什么我的Django网站无法正常工作?

时间:2013-11-30 16:42:22

标签: python ajax django

我编写了一个Django Web应用程序,用于解析来自其他站点的HTML。解析的结果应该通过Ajax作为JSON返回。但是,该网站没有任何工作;即,它没有返回任何东西。当前端调用后端时,XHR对象返回,因为找不到404。这有什么问题?

urls.py

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

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

views.py:

"""
Parser written by Tyler Schade. This was written for the BoT Media Group,
for the launch of our debut app.
If you have ANY QUESTIONS WHATSOEVER, please feel
FREE to email me at tylerschade99@gmail.com. See the
comments in the code for for info.
"""

"""
This Parser is written in Python, and utilizes BeautifulSoup3 and the urllib.
The question comes: Why BS3?
BS4 completely trashed the code, and didn't
parse it correctly. So I am using BS3.
"""

#import the librarys we need
from django.http import HttpResponse
import re
from BeautifulSoup import BeautifulSoup
import urllib
import json


def canadian_time(request):
    #Calling the HTML Source from the URL
    sock = urllib.urlopen("http://www.cbsa-asfc.gc.ca/bwt-taf/menu-eng.html")
    htmlSource = sock.read()
    sock.close()
    #create a soup object
soup = BeautifulSoup(htmlSource)
#find the tags we need and their content
tunnel = soup('td', headers="Trav TravCanada", limit=19)[17]
bridge = soup('td', headers="Trav TravCanada", limit=19)[18]
#new variables to be passed
contents_of_tunnel = tunnel.string
contents_of_bridge = bridge.string
#use a regex to just get the numbers -- not the rest of the string
number_inside_of_tunnel = re.findall(r"\d+", contents_of_tunnel)
number_inside_of_bridge = re.findall(r"\d+", contents_of_bridge)
#make the regexes into variables
delay_for_tunnel_string = number_inside_of_tunnel[0]
delay_for_bridge_string = number_inside_of_bridge[0]
#convert the variables into ints, not strings
delay_for_tunnel = int(delay_for_tunnel_string)
delay_for_bridge = int(delay_for_bridge_string)
#start defining the final values
if contents_of_tunnel == "No Delay":
    time_to_cross_the_tunnel = 10
else:
    time_to_cross_the_tunnel = delay_for_tunnel + 10
if contents_of_bridge == "No Delay":
    time_to_cross_the_bridge = 10
else:
    time_to_cross_the_bridge = delay_for_bridge + 10
#convert to JSON
to_return = json.dumps({'bridge_time': time_to_cross_the_bridge,
                       'tunnel_time': time_to_cross_the_tunnel},
                       separators=(',', ': '))
#finally, return as Ajax
return HttpResponse(to_return)


def american_time(request):
#Calling the HTML Source from the URL
sock = urllib.urlopen("http://apps.cbp.gov/bwt/index.asp")
htmlSource = sock.read()
sock.close()
#create a soup object
soup = BeautifulSoup(htmlSource)
#find the tags we need and their content
bridge = soup.findAll('td', limit=215)[194]
tunnel = soup.findAll('td', limit=250)[208]
#new variables to be passed
contents_of_tunnel = tunnel.getText(', ')
contents_of_bridge = bridge.getText(', ')
#check to see if there is a delay for the bridge
if 'no delay' in contents_of_bridge:
    time_to_cross_the_bridge = 0
else:
    inside_of_bridge = re.split(r', ', contents_of_bridge)
    number_inside_of_bridge = inside_of_bridge[1]
    list_for_time_to_cross_the_bridge = re.findall(r"\d+", number_inside_of_bridge)
    time_to_cross_the_bridge = list_for_time_to_cross_the_bridge[0]
if 'no delay' in contents_of_tunnel:
    time_to_cross_the_tunnel = 0
else:
    inside_of_tunnel = re.split(r', ', contents_of_tunnel)
    number_inside_of_tunnel = inside_of_tunnel[1]
    list_for_time_to_cross_the_tunnel = re.findall(r"\d+", number_inside_of_tunnel)
    time_to_cross_the_tunnel = list_for_time_to_cross_the_tunnel[0]
#convert to JSON
to_return = json.dumps({'bridge_time': time_to_cross_the_bridge,
                       'tunnel_time': time_to_cross_the_tunnel},
                       separators=(',', ': '))
#finally, return as Ajax
return HttpResponse(to_return)

和Ajax:

向AWS发送post请求,然后 //将数据插入路径

$.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.");
  });
});

0 个答案:

没有答案