Pinax源代码分析11 - tweets
微博客,第三方 app django_microblogging。
url
(r'^tweets/', include('microblogging.urls')),
逻辑与模板
urlpatterns = patterns('',
#显示当前用户所有的following的tweets,负责信添加一条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里找一下,果然:
删除“记住”的内容,重新发送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),