当前位置:K88软件开发文章中心网站服务器框架django → 文章内容

Django 输出非HTML内容

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-25 14:19:24

eptions.ObjectDoesNotExist if given invalid parameters. 在 Beat.objects.get() 调用中也没有出现 try /except 代码块。 函数在出错时抛出 Beat.DoesNotExist 异常,而 Beat.DoesNotExist是 ObjectDoesNotExist 异常的一个子类型。为产生  ,  , 和  的feeds, Django使用 title() , link() , 和description() 方法。 在上面的例子中,它们都是简单的字符串类型的类属性,而这个例子表明,它们既可以是字符串, 也可以是 方法。 对于每一个 title , link 和 description 的组合,Django使用以下的算法:试图调用一个函数,并且以 get_object() 返回的对象作为参数传递给 obj 参数。如果没有成功,则不带参数调用一个方法。还不成功,则使用类属性。最后,值得注意的是,这个例子中的 items() 使用 obj 参数。 对于 items 的算法就如同上面第一步所描述的那样,首先尝试 items(obj) , 然后是 items() ,最后是 items 类属性(必须是一个列表)。Feed 类所有方法和属性的完整文档,请参考官方的Django文档 (http://www.djangoproject.com/documentation/0.96/syndication_feeds/) 。指定Feed的类型默认情况下, 聚合框架生成RSS 2.0. 要改变这样的情况, 在 Feed 类中添加一个 feed_type 属性. To change that, add a feed_type attribute to your Feed class:from django.utils.feedgenerator import Atom1Feedclass MyFeed(Feed): feed_type = Atom1Feed注意你把 feed_type 赋值成一个类对象,而不是类实例。 目前合法的Feed类型如表11-1所示。表 11-1. Feed 类型Feed 类类型django.utils.feedgenerator.Rss201rev2FeedRSS 2.01 (default)django.utils.feedgenerator.RssUserland091FeedRSS 0.91django.utils.feedgenerator.Atom1FeedAtom 1.0闭包为了指定闭包(例如,与feed项比方说MP3 feeds相关联的媒体资源信息),使用 item_enclosure_url ,item_enclosure_length , 以及 item_enclosure_mime_type ,比如from myproject.models import Songclass MyFeedWithEnclosures(Feed): title = "Example feed with enclosures" link = "/feeds/example-with-enclosures/" def items(self): return Song.objects.all()[:30] def item_enclosure_url(self, item): return item.song_url def item_enclosure_length(self, item): return item.song_length item_enclosure_mime_type = "audio/mpeg"当然,你首先要创建一个包含有 song_url 和 song_length (比如按照字节计算的长度)域的 Song 对象。语言聚合框架自动创建的Feed包含适当的  标签(RSS 2.0) 或 xml:lang 属性(Atom). 他直接来自于您的LANGUAGE_CODE 设置. This comes directly from your LANGUAGE_CODE setting.URLslink 方法/属性可以以绝对URL的形式(例如, "/blog/" )或者指定协议和域名的URL的形式返回(例如"http://www.example.com/blog/" )。如果 link 没有返回域名,聚合框架会根据 SITE_ID 设置,自动的插入当前站点的域信息。 (See Chapter 16 for more on SITE_ID and the sites framework.)Atom feeds需要  rel="self"> 指明feeds现在的位置。 The syndication framework populates this automatically.同时发布Atom and RSS一些开发人员想 同时 支持Atom和RSS。 这在Django中很容易实现: 只需创建一个你的 feed 类的子类,然后修改 feed_type ,并且更新URLconf内容。 下面是一个完整的例子: Here’s a full example:from django.contrib.syndication.feeds import Feedfrom django.utils.feedgenerator import Atom1Feedfrom mysite.blog.models import Entryclass RssLatestEntries(Feed): title = "My Blog" link = "/archive/" description = "The latest news about stuff." def items(self): return Entry.objects.order_by('-pub_date')[:5]class AtomLatestEntries(RssLatestEntries): feed_type = Atom1Feed这是与之相对应那个的URLconf:from django.conf.urls.defaults import *from myproject.feeds import RssLatestEntries, AtomLatestEntriesfeeds = {'rss': RssLatestEntries,'atom': AtomLatestEntries,}urlpatterns = patterns('',...(r'^feeds/(?P.*)//pre>, 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),# ...)Sitemap 框架sitemap 是你服务器上的一个XML文件,它告诉搜索引擎你的页面的更新频率和某些页面相对于其它页面的重要性。 这个信息会帮助搜索引擎索引你的网站。例如,这是 Django 网站(http://www.djangoproject.com/sitemap.xml)sitemap的一部分:http://www.djangoproject.com/documentation/weekly0.5http://www.djangoproject.com/documentation/0_90/never0.1...需要了解更多有关 sitemaps 的信息, 请参见 http://www.sitemaps.org/.Django sitemap 框架允许你用 Python 代码来表述这些信息,从而自动创建这个XML文件。 要创建一个站点地图,你只需要写一个Sitemap 类,并且在URLconf中指向它。安装要安装 sitemap 应用程序, 按下面的步骤进行:将 'django.contrib.sitemaps' 添加到您的 INSTALLED_APPS 设置中.确保 'django.template.loaders.app_directories.load_template_source' 在您的 TEMPLATE_LOADERS 设置中。 默认情况下它在那里, 所以, 如果你已经改变了那个设置的话, 只需要改回来即可。确定您已经安装了 sites 框架 (参见第14章).Notesitemap 应用程序没有安装任何数据库表. 它需要加入到 INSTALLED_APPS 中的唯一原因是: 这样load_template_source 模板加载器可以找到默认的模板. The only reason it needs to go intoINSTALLED_APPS is so the load_template_source template loader can find the default templates.Initialization要在您的Django站点中激活sitemap生成, 请在您的 URLconf 中添加这一行:(r'^sitemap.xml/pre>, 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})This line tells Django to build a sitemap when a client accesses /sitemap.xml . Note that the dot character in sitemap.xml is escaped with a backslash, because dots have a special meaning in regular expressions.sitemap文件的名字无关紧要,但是它在服务器上的位置却很重要。 搜索引擎只索引你的sitemap中

上一页  [1] [2] [3] [4] [5] [6]  下一页


Django 输出非HTML内容