Ticket #10907: news_sitemaps.diff
File news_sitemaps.diff, 9.8 KB (added by , 16 years ago) |
---|
-
django/contrib/sitemaps/views.py
22 22 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) 23 23 return HttpResponse(xml, mimetype='application/xml') 24 24 25 def sitemap(request, sitemaps, section=None ):25 def sitemap(request, sitemaps, section=None, template='sitemap.xml'): 26 26 maps, urls = [], [] 27 27 if section is not None: 28 28 if section not in sitemaps: … … 41 41 raise Http404("Page %s empty" % page) 42 42 except PageNotAnInteger: 43 43 raise Http404("No page '%s'" % page) 44 xml = smart_str(loader.render_to_string( 'sitemap.xml', {'urlset': urls}))44 xml = smart_str(loader.render_to_string(template, {'urlset': urls})) 45 45 return HttpResponse(xml, mimetype='application/xml') -
django/contrib/sitemaps/__init__.py
38 38 # http://sitemaps.org/protocol.php#index. 39 39 limit = 50000 40 40 41 def _ _get(self, name, obj, default=None):41 def _get(self, name, obj, default=None): 42 42 try: 43 43 attr = getattr(self, name) 44 44 except AttributeError: … … 59 59 return self._paginator 60 60 paginator = property(_get_paginator) 61 61 62 def get_url_info(self, item, current_site): 63 loc = "http://%s%s" % (current_site.domain, self._get('location', item)) 64 url_info = { 65 'location': loc, 66 'lastmod': self._get('lastmod', item, None), 67 'changefreq': self._get('changefreq', item, None), 68 'priority': self._get('priority', item, None) 69 } 70 return url_info 71 62 72 def get_urls(self, page=1): 63 73 from django.contrib.sites.models import Site 64 74 current_site = Site.objects.get_current() 65 75 urls = [] 66 76 for item in self.paginator.page(page).object_list: 67 loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) 68 url_info = { 69 'location': loc, 70 'lastmod': self.__get('lastmod', item, None), 71 'changefreq': self.__get('changefreq', item, None), 72 'priority': self.__get('priority', item, None) 73 } 74 urls.append(url_info) 77 urls.append(self.get_url_info(item, current_site)) 75 78 return urls 76 79 77 80 class FlatPageSitemap(Sitemap): … … 98 101 if self.date_field is not None: 99 102 return getattr(item, self.date_field) 100 103 return None 104 105 class NewsSitemap(Sitemap): 106 # This limit is defined by Google. See the index documentation at 107 # http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288 108 limit = 1000 109 110 111 def get_url_info(self, item, current_site): 112 url_info = super(NewsSitemap, self).get_url_info(item, current_site) 113 url_info.update({ 114 'publication_date': self._get('publication_date', item, None), 115 'keywords': self._get('keywords', item, None), 116 }) 117 return url_info -
django/contrib/sitemaps/templates/news_sitemap.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <urlset 3 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 4 xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"> 5 {% spaceless %} 6 {% for url in urlset %} 7 <url> 8 <loc>{{ url.location }}</loc> 9 {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %} 10 {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %} 11 {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %} 12 <news:news> 13 {% if url.publication_date %}<news:publication_date>{{ url.publication_date|date:"Y-m-d" }}</news:publication_date>{% endif %} 14 {% if url.keywords %}<news:keywords>Business, Mergers, Acquisitions</news:keywords>{% endif %} 15 </news:news> 16 </url> 17 {% endfor %} 18 {% endspaceless %} 19 </urlset> -
docs/ref/contrib/sitemaps.txt
Property changes on: django/contrib/sitemaps/templates/news_sitemap.xml ___________________________________________________________________ Added: svn:eol-style + native
8 8 :synopsis: A framework for generating Google sitemap XML files. 9 9 10 10 Django comes with a high-level sitemap-generating framework that makes 11 creating sitemap_ XML files easy.11 creating sitemap_ and newssitemap_ XML files easy. 12 12 13 13 .. _sitemap: http://www.sitemaps.org/ 14 .. _newssitemap: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288 14 15 15 16 Overview 16 17 ======== … … 20 21 to other pages on your site. This information helps search engines index your 21 22 site. 22 23 23 The Django sitemap framework automates the creation of this XML file by letting 24 A Google News sitemap is an XML file that allows you to have more control over 25 the content you submit to Google News. Creating a News sitemap allows you to 26 tell Google about all your news articles, their publication date and keywords. 27 A news sitemaps differs only slightly from a normal sitemap. The most important 28 difference is the requirement for your site to register (and be accepted) into 29 the list of news sites that google news indexes. 30 31 The Django sitemap framework automates the creation of these XML files by letting 24 32 you express this information in Python code. 25 33 26 34 It works much like Django's :ref:`syndication framework 27 35 <ref-contrib-syndication>`. To create a sitemap, just write a 28 :class:`~django.contrib.sitemaps.Sitemap` class and point to it in your 36 :class:`~django.contrib.sitemaps.Sitemap` and/or 37 :class:`~django.contrib.sitemaps.Sitemap` class and point to them in your 29 38 :ref:`URLconf <topics-http-urls>`. 30 39 31 40 Installation … … 51 60 Initialization 52 61 ============== 53 62 54 To activate sitemap generation on your Django site, add th is lineto your63 To activate sitemap generation on your Django site, add the applicable lines to your 55 64 :ref:`URLconf <topics-http-urls>`:: 56 65 57 66 (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) 58 67 59 68 This tells Django to build a sitemap when a client accesses :file:`/sitemap.xml`. 60 69 70 For a google news sitemap, the setup is nearly identical 71 :ref:`URLconf <topics-http-urls>`:: 72 73 (r'^news_sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': news_sitemaps, 'template': 'news_sitemap.xml'}) 74 75 This tells Django to build a news sitemap when a client accesses :file:`/news_sitemap.xml`. 76 61 77 The name of the sitemap file is not important, but the location is. Search 62 78 engines will only index links in your sitemap for the current URL level and 63 79 below. For instance, if :file:`sitemap.xml` lives in your root directory, it may … … 67 83 68 84 The sitemap view takes an extra, required argument: ``{'sitemaps': sitemaps}``. 69 85 ``sitemaps`` should be a dictionary that maps a short section label (e.g., 70 ``blog`` or `` news``) to its :class:`~django.contrib.sitemaps.Sitemap` class71 (e.g., ``BlogSitemap`` or `` NewsSitemap``). It may also map to an *instance* of86 ``blog`` or ``events``) to its :class:`~django.contrib.sitemaps.Sitemap` class 87 (e.g., ``BlogSitemap`` or ``EventSitemap``). It may also map to an *instance* of 72 88 a :class:`~django.contrib.sitemaps.Sitemap` class (e.g., 73 89 ``BlogSitemap(some_var)``). 74 90 91 The sitemap framework takes an extra, optional argument: ``{'template': 'news_sitemap.xml'}``. 92 The :file:`news_sitemap.xml` generates a Google News Sitemap. When using this 93 template, the 'sitemaps' argument should only contain 94 :class:`~django.contrib.sitemaps.NewsSitemap` classes (or instances). 95 Because :class:`~django.contrib.sitemaps.NewsSitemap` extends 96 :class:`~django.contrib.sitemaps.Sitemap` the rest of this documentaiton will 97 only refer to those attributes that are unique to the 98 :class:`~django.contrib.sitemaps.NewsSitemap` classes (or instances). 99 75 100 Sitemap classes 76 101 =============== 77 102 … … 209 234 210 235 .. _sitemaps.org documentation: http://www.sitemaps.org/protocol.html#prioritydef 211 236 237 NewsSitemap class reference 238 ======================= 239 240 .. class:: NewsSitemap 241 242 A ``NewsSitemap`` class can define all of the following methods/attributes in addition to those above: 243 244 .. attribute:: Sitemap.items 245 246 **Required.** A method that returns a list of objects. (See :class:`~django.contrib.sitemaps.Sitemap`) 247 In the case of a news sitemap, this method must only return news items from the past 3 days. 248 249 .. attribute:: Sitemap.publication_date 250 251 **Required.** A method that takes on argument -- an object as returned by 252 :attr:`~Sitemap.items()` -- and return that object's published_date, as a Python 253 ``datetime.datetime`` object. 254 255 .. attribute:: Sitemap.keywords 256 257 **Optional.** Either a method or attribute. 258 259 If it's a method, it should return a string containing the comma-separated list of 260 keywords describing the content of the article returned by :attr:`~Sitemap.items()`. 261 262 If it's an attribute, its value should be a string containing the comma-separated list 263 of keywords describing the content of the article returned by :attr:`~Sitemap.items()`. 264 265 In both cases, the keywords may drawn from, but not limited to _googlenewscategories. 266 267 268 .. _googlenewscategories: http://www.google.com/support/webmasters/bin/answer.py?answer=42993 269 212 270 Shortcuts 213 271 ========= 214 272