Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6208039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:44:22+00:00 2026-05-24T05:44:22+00:00

I put together a function to link to news items with django as a

  • 0

I put together a function to link to news items with django as a filter. It works on dev_appserver but on production server it returns None Can you tell me why it’s not working? Should I investigate the except clause of the code where it currently just passes?

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)

Update: Now it returns an empty string on production but locally it works. It’s something else that status 200 on production:

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 ''

EDIT: Here’s the simplest reproduction that return a status 200 locally and a status 503 on production

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

Update: Here’s the solution I currently use. It still needs improfement since there is a possibility for choosing 2 news items that are the same:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:44:23+00:00Added an answer on May 24, 2026 at 5:44 am

    Python functions that don’t explicitly return anything will return None. If this code is returning None on your production server, it’s probably because it’s hitting that last except: pass block, as you mentioned.

    Without reading the actual code (which I haven’t), I’d say replace that pass with return '' to safely swallow the urlfetch.Error or decide what you want to happen in that case and implement some new code for that block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have put together the following mootools script window.addEvent('domready', function() { var shouts =
I've just put together this regExp function that checks the contents of a string.
I'm trying to understand the syntax of how to put together a JavaScript function
How would I put together a PHP5 function that would find the current calendar
I'm trying to put together an application which uses YUI's DataTable component but I
I've put together the function below. It's supposed to take in a nested (multi-tiered)
I've put together a function that creates a sharepoint folder in a document library
I am trying to put together a function that does the following: retrieve a
I put together a sample scenario of my issue and I hope its enough
I put together a class yesterday to do some useful task. I started alpha

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.