0
我想將保存的鏈接保存到我的數據庫中,以便在後續運行時僅刪除並追加新鏈接。Python/Django將自動保存的數據自動保存到我的數據庫中
這是我的代碼在下面,但在一天結束時我的數據庫是空的。我能做些什麼改變來克服這一點?在此先感謝
from django.template.loader import get_template
from django.shortcuts import render_to_response
from bs4 import BeautifulSoup
import urllib2, sys
import urlparse
import re
from listing.models import jobLinks
#this function extract the links
def businessghana():
site = "http://www.businessghana.com/portal/jobs"
hdr = {'User-Agent' : 'Mozilla/5.0'}
req = urllib2.Request(site, headers=hdr)
jobpass = urllib2.urlopen(req)
soup = BeautifulSoup(jobpass)
for tag in soup.find_all('a', href = True):
tag['href'] = urlparse.urljoin('http://www.businessghana.com/portal/', tag['href'])
return map(str, soup.find_all('a', href = re.compile('.getJobInfo')))
# result from businssghana() saved to a variable to make them iterable as a list
all_links = businessghana()
#this function should be saving the links to the database unless the link already exist
def save_new_links(all_links):
current_links = jobLinks.objects.all()
for i in all_links:
if i not in current_links:
jobLinks.objects.create(url=i)
# I called the above function here hoping that it will save to database
save_new_links(all_links)
# return my httpResponse with this function
def display_links(request):
name = all_links()
return render_to_response('jobs.html', {'name' : name})
我的Django的models.py如下:
from django.db import models
class jobLinks(models.Model):
links = models.URLField()
pub_date = models.DateTimeField('date retrieved')
def __unicode__(self):
return self.links
你有'debug = True'嗎?你有什麼錯誤嗎? – sundance