2014-01-26 75 views
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 
+0

你有'debug = True'嗎?你有什麼錯誤嗎? – sundance

回答

0

你必須與你的代碼的幾個問題。首先,jobLinks型號沒有url字段。相反,您應該在create()語句中使用links=i。其次,你正在檢查一個字符串url是否在QuerySet中。這絕不會是這種情況,因爲QuerySet的元素是模型類的對象,在您的情況下爲jobLinks對象。相反,你可以做這樣的事情:

def save_new_links(all_links): 
    new_links = [] 
    for i in all_links: 
     if not jobLinks.objects.filter(links=i): 
      new_links.append(jobLinks(links=i)) 
    jobLinks.objects.bulk_create(new_links) 

我還建議,在將來,你會爲你的模型使用單數的單詞。例如,在這種情況下,我會呼叫模型jobLink和字段link