跳转到主要内容

为django提供/消费富媒体

项目描述

安装

首先,您需要安装OEmbed。它可在http://github.com/worldcompany/djangoembed/找到

git clone git://github.com/worldcompany/djangoembed/
cd djangoembed
python setup.py install

添加到您的Django项目

安装后,将OEmbed消费添加到您的项目中非常简单。首先,将其添加到项目的INSTALLED_APPs中并运行'syncdb'

# settings.py
INSTALLED_APPS = [
    ...
    'oembed'
]

djangoembed使用与admin相似的注册模式。为了确保所有应用程序都已加载,djangoembed应在urls.py中运行autodiscover()。如果您喜欢,可以将此代码放置在admin.autodiscover()部分下方

# urls.py
import oembed
oembed.autodiscover()

消费资源

现在您已准备好开始消费可嵌入的OEmbed对象。根据您想要做什么,有几个选项。开始运行的最直接方法是将其添加到模板中

{% load oembed_tags %}

{% oembed %}blog.content{% endoembed %}

{# or use the filter #}

{{ blog.content|oembed }}

{# maybe you're working with some dimensional constraints #}

{% oembed "600x600" %}blog.content{% endoembed %}

{{ blog.content|oembed:"600x600" }}

您也可以在Python中使用oembed对象

import oembed
oembed.autodiscover()

# just get the metadata
resource = oembed.site.embed('http://www.youtube.com/watch?v=nda_OSWeyn8')
resource.get_data()

{u'author_name': u'botmib',
 u'author_url': u'http://www.youtube.com/user/botmib',
 u'height': 313,
 u'html': u'<object width="384" height="313"><param name="movie" value="http://www.youtube.com/v/nda_OSWeyn8&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/nda_OSWeyn8&fs=1" type="application/x-shockwave-flash" width="384" height="313" allowscriptaccess="always" allowfullscreen="true"></embed></object>',
 u'provider_name': u'YouTube',
 u'provider_url': u'http://www.youtube.com/',
 u'title': u'Leprechaun in Mobile, Alabama',
 u'type': u'video',
 u'version': u'1.0',
 u'width': 384}

# get the metadata and run it through a template for pretty presentation
from oembed.consumer import OEmbedConsumer
client = OEmbedConsumer()
embedded = client.parse_text("http://www.youtube.com/watch?v=nda_OSWeyn8")

<div class="oembed oembed-video provider-youtube">
  <object width="384" height="313">
    <param name="movie" value="http://www.youtube.com/v/nda_OSWeyn8&fs=1"></param>
    <param name="allowFullScreen" value="true"></param>
    <param name="allowscriptaccess" value="always"></param>
    <embed src="http://www.youtube.com/v/nda_OSWeyn8&fs=1"
           type="application/x-shockwave-flash"
           width="384"
           height="313"
           allowscriptaccess="always"
           allowfullscreen="true">
    </embed>
  </object>
  <p class="credit">
    <a href="http://www.youtube.com/watch?v=nda_OSWeyn8">Leprechaun in Mobile, Alabama</a>
    by
    <a href="http://www.youtube.com/user/botmib">botmib</a>
  </p>
</div>'

故障排除

问题:您尝试youtube嵌入示例,但您得到的是youtube视频的链接。

解决方案:djangoembed使用固件将有关oembed提供者(如Youtube)的数据加载到数据库中。尝试使用syncdb(或如果您正在运行South,则使用迁移)进行调整,直到有oembed.storedprovider类型的对象。

如果您还有其他问题,请考虑查阅项目doc子目录中的更详细文档。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

djangoembed-0.1.1.tar.gz (114.2 kB 查看哈希值)

上传时间: 源代码

由以下机构支持