Pinax源代码分析11 - tweets

Pinax源代码分析11 tweets

微博客,第三方 app django_microblogging

url

    (r'^tweets/', include('microblogging.urls')),


逻辑与模板

urlpatterns = patterns('',
    #
显示当前用户所有的followingtweets,负责信添加一条tweet

    url(r'^$', 'microblogging.views.personal', name='tweets_you_follow'),
    #
全站所有的tweets
    url(r'^all/$', 'microblogging.views.public', name='all_tweets'),
    #
查看单独的一条tweet,但是除了直接访问url外没找到其它入口

    url(r'^(/d+)/$', 'microblogging.views.single', name='single_tweet'),
    #
显示follow该用户和被该用户follow的人。
    url(r'^followers/(/w+)/$', 'microblogging.views.followers', name='tweet_followers'),
    url(r'^following/(/w+)/$', 'microblogging.views.following', name='tweet_following'),
    #
开始或者停止follow某人
    url(r'^toggle_follow/(/w+)/$', 'microblogging.views.toggle_follow', name='toggle_follow'),
)

模板中自有listing.html有点看透,里面包含了oembed_tags。测试了一下,居然报错……TemplateSyntaxError at /tweets/

Caught an exception while rendering: oembed/link.html

Request Method:

GET

Request URL:

http://localhost:8000/tweets/

Exception Type:

TemplateSyntaxError

Exception Value:

Caught an exception while rendering: oembed/link.html

Exception Location:

/root/workspace/pinax/lib/python2.6/site-packages/Django-1.0.2_final-py2.6.egg/django/template/debug.py in render_node, line 81

Python Executable:

/root/workspace/pinax/bin/python

Python Version:

2.6.2



将模板中的{{ tweet.html|safe|oembed|urlize }}修改为{{ tweet.html|safe|urlize }}一切正常,可见是 oembed_tags.oembed 抛出的异常。google了一下,没找到啥,自己动手吧。
输入tweet的内容为:
    Look at this! http://en.wikipedia.org/wiki/China
根据出错信息推测,应该是和 oembed/link.html 这个模板有关,到处也没有找到这个模板。尝试这新建一个空白的link.html,果然不再出错了,但是http://en.wikipedia.org/wiki/China 莫名消失。研究了一下已有的oembed模板photo.html, rich.html, video.html 都围绕着一个response 变量。所以将link.html的内容修改为:
{% autoescape off %}{{ response }}{% endautoescape %}
重新浏览,发现内容没有改变。看来oembed是在发送的时候转换内容,显示内容的时候就不再做转换了,这和blog恰好相反,我觉得这个方法比较节省性能。重新发送内容,居然结果仍然没有改变。看来,将http://en.wikipedia.org/wiki/China替换为空这条规则已经被Pinax“记住了。在admin里找一下,果然:

Pinax oembed数据库缓存 

删除记住的内容,重新发送tweet。输出如下:
12 Look at this! {'provider_name': 'Wikipedia', 'version': '1.0', 'type': 'link', 'html': u'

China (traditional Chinese: /u4e2d/u570b; simplified Chinese: /u4e2d/u56fd; Tongyong Pinyin: Jhonggu/xf3; Hanyu Pinyin: Zh/u014dnggu/xf3 (help/xb7info); Wade-Giles (Mandarin): Chung/xb9kuo/xb2) is a cultural region, an ancient civilization, and, depending on perspective, a national or multinational entity extending over a large area in East Asia.

……

……

estimated to have lived approximately from 300,000 to 550,000 years ago.



可见,oembed是根据 response 字典的 type 的值来匹配模板。修改link.html如下即可正常显示内容了:

{% autoescape off %}
    <strong>{{ response.provider_name }},{{ response.title }}</strong>:
    {{ response.html }}
{% endautoescape %}

同时,需要更正上面的推测,看来oembed并不是在发消息的时候进行转换,而是在显示的时候转换,并且首先看看自己的记忆中是否有该条数据,如果没有就进行转换。

Feeds

同样使用的是atomformat

from microblogging.feeds import TweetFeedAll, TweetFeedUser, TweetFeedUserWithFriends
tweets_feed_dict = {"feed_dict": {
    'all': TweetFeedAll,
    'only': TweetFeedUser,
    'with_friends': TweetFeedUserWithFriends,
}}

    (r'^feeds/tweets/(.*)/$', 'django.contrib.syndication.views.feed', tweets_feed_dict),




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值