为什么我的django片段不起作用?

时间:2011-07-30 07:56:28

标签: python django google-app-engine

我把一个函数链接到django作为过滤器的新闻项目。它适用于dev_appserver但在生产服务器上它返回None你能告诉我它为什么不起作用吗?我应该调查当前刚刚通过的代码的except子句吗?

def news(n):
    url = os.environ.get('HTTP_HOST') if os.environ.get('HTTP_HOST') else os.environ['SERVER_NAME']
    tld = url[url.rfind('.'):]    
    try:        
        if url == 'localhost:8080':
            result = urlfetch.fetch('http://news.google.se/?output=rss')    
        elif tld != '.com' and tld != '.se' and tld != '.cl' :
            result = urlfetch.fetch('http://news.google.com'+tld+'/?output=rss') 
        else:      
            result = urlfetch.fetch('http://news.google.com/?output=rss')        
        if result.status_code == 200:
            dom = minidom.parseString(result.content)
            item_node = dom.getElementsByTagName("item")
            try:
                random_1=random.choice(item_node)
                rss1_link = random_1.childNodes[1].firstChild.data
                rss1_text = random_1.childNodes[0].firstChild.data
                return mark_safe('<a href="%s">%s</a>' % (rss1_link, rss1_text))
            except IndexError,e:
                return ''
    except urlfetch.Error, e:
        pass

register.filter(news)

更新:现在它在生产时返回一个空字符串,但在本地它可以工作。这是生产状态200的另一个原因:

def news(n):
    url = os.environ.get('HTTP_HOST') if os.environ.get('HTTP_HOST') else os.environ['SERVER_NAME']
    tld = url[url.rfind('.'):]    
    try:        
        if url == 'localhost:8080':
            result = urlfetch.fetch('http://news.google.se/?output=rss')    
        elif tld != '.com' and tld != '.se' and tld != '.cl' :
            result = urlfetch.fetch('http://news.google.com'+tld+'/?output=rss') 
        else:        
            result = urlfetch.fetch('http://news.google.com/?output=rss')        
        if result.status_code == 200:
            dom = minidom.parseString(result.content)
            item_node = dom.getElementsByTagName("item")
            try:
                random_1=random.choice(item_node)
                rss1_link = random_1.childNodes[1].firstChild.data
                rss1_text = random_1.childNodes[0].firstChild.data
                return mark_safe('<a href="%s">%s</a>' % (rss1_link, rss1_text))        
            except IndexError,e:
                return ''
        else:
            return ''
    except urlfetch.Error, e:
        logging.error(str(e))
        return ''

编辑:这是最简单的复制,它在本地返回状态200,在生产中返回状态503

def status(n):
    try:             
        result = urlfetch.fetch('http://news.google.com/?output=rss')       
        return str(result.status_code)
    except urlfetch.Error, e:
        return 'error'

更新:这是我目前使用的解决方案。它仍然需要无效,因为有可能选择2个相同的新闻项目:

import random
def updateFeed(url):#to do, get srv from url and find number of entries
    srv = os.environ.get('HTTP_HOST') if os.environ.get('HTTP_HOST') else os.environ['SERVER_NAME']
    tld = srv[srv.rfind('.'):] 
    url = 'http://news.google.com/?output=rss'
    if srv.endswith('.com.br'):
        url = 'http://news.google.com.br/?output=rss'
    elif srv == 'localhost:8080' or srv.endswith('alltfunkar.com'):
        url = 'http://news.google.se/?output=rss'
    elif tld != '.com' and tld != '.se' and tld != '.cl' :
        url = 'http://news.google.com'+tld+'/?output=rss'
    query_args = { 'q': url, 'v':'1.0', 'num': '15', 'output': 'json' }
    qs = urllib.urlencode(query_args)
    loader = 'http://ajax.googleapis.com/ajax/services/feed/load'
    loadurl = '%s?%s' % (loader, qs)
    logging.info(loadurl)
    result = urlfetch.fetch(url=loadurl,headers={'Referer': '...'})
    if result.status_code == 200:
        news = simplejson.loads(result.content) 

        """ not working, using random.randrange instead
        some_key = random.choice(news.keys())
        something = news[some_key]
        """
        i = random.randrange(0,10)#to do: instead of 10, it should be number of entries
        title = news[u'responseData'][u'feed'][u'entries'][i][u'title']
        link = news[u'responseData'][u'feed'][u'entries'][i][u'link']
    return mark_safe('<a href="%s">%s</a>' % (link, title))

1 个答案:

答案 0 :(得分:2)

未明确return任何内容的Python函数将返回None。如果此代码在生产服务器上返回None,则可能是因为它正在触及最后except: pass块,如您所述。

如果没有阅读实际代码(我没有阅读),我会说pass替换为return ''以安全地吞下urlfetch.Error 在这种情况下决定你想要发生什么,并为该块实现一些新代码。