开发者

How to update a django filter to django 1.2 with GAE

开发者 https://www.devze.com 2023-02-14 19:30 出处:网络
Hi I got the following filter which worked with django 0.96 (GAE) and is generating code on the layout erroneoulsy with GAE + django 1.2. Now i want to update the filter so that it works with django 1

Hi I got the following filter which worked with django 0.96 (GAE) and is generating code on the layout erroneoulsy with GAE + django 1.2. Now i want to update the filter so that it works with django 1.2. COuld you provide any information please kindly do so.

Filter:

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' or url.endswith('alltfunkar.com'):
            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'+tld+'/?output=rss')
            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:
      开发者_C百科          random_1=random.choice(item_node)
                rss1_link = random_1.childNodes[1].firstChild.data
                rss1_text = random_1.childNodes[0].firstChild.data
                return '<a href="'+rss1_link+'">'+rss1_text+'</a>'
            except IndexError,e:
                return ''
    except urlfetch.Error, e:
        pass

register.filter(news)

Template usage

{{a|news|fix_ampersands|truncatewords_html:8}

Solved and done with help here! Many thanks:

How to update a django filter to django 1.2 with GAE


Of course it would have helped if you'd specified exactly what was "erroneous" about the output you're getting.

My guess is that it's autoescaped, as you would know if you'd read the guide to upgrading from 0.96 to 1.0. Mark the output as safe:

from django.utils.safestring import mark_safe
...
return mark_safe('<a href="'+rss1_link+'">'+rss1_text+'</a>')

or, more Pythonically:

return mark_safe('<a href="%s">%s</a>' % (rss1_link, rss2_text))
0

精彩评论

暂无评论...
验证码 换一张
取 消