Ticket #14041: 14041_sitemap_templates.diff
File 14041_sitemap_templates.diff, 7.7 KB (added by , 14 years ago) |
---|
-
django/contrib/sitemaps/tests/basic.py
1 import os 1 2 from datetime import date 2 3 from django.conf import settings 3 4 from django.contrib.auth.models import User … … 16 17 def setUp(self): 17 18 self.old_USE_L10N = settings.USE_L10N 18 19 self.old_Site_meta_installed = Site._meta.installed 20 self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS 21 settings.TEMPLATE_DIRS = ( 22 os.path.join(os.path.dirname(__file__), 'templates'), 23 ) 19 24 # Create a user that will double as sitemap content 20 25 User.objects.create_user('testuser', 'test@example.com', 's3krit') 21 26 22 27 def tearDown(self): 23 28 settings.USE_L10N = self.old_USE_L10N 24 29 Site._meta.installed = self.old_Site_meta_installed 30 settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS 25 31 32 def test_simple_sitemap_index(self): 33 "A simple sitemap index can be rendered" 34 # Retrieve the sitemap. 35 response = self.client.get('/simple/index.xml') 36 # Check for all the important bits: 37 self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> 38 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 39 <sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap> 40 </sitemapindex> 41 """) 42 43 def test_simple_sitemap_custom_index(self): 44 "A simple sitemap index can be rendered with a custom template" 45 # Retrieve the sitemap. 46 response = self.client.get('/simple/custom-index.xml') 47 # Check for all the important bits: 48 self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> 49 <!-- This is a customised template --> 50 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 51 <sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap> 52 </sitemapindex> 53 """) 54 26 55 def test_simple_sitemap(self): 27 56 "A simple sitemap can be rendered" 28 57 # Retrieve the sitemap. … … 34 63 </urlset> 35 64 """ % date.today().strftime('%Y-%m-%d')) 36 65 66 def test_simple_custom_sitemap(self): 67 "A simple sitemap can be rendered with a custom template" 68 # Retrieve the sitemap. 69 response = self.client.get('/simple/custom-sitemap.xml') 70 # Check for all the important bits: 71 self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> 72 <!-- This is a customised template --> 73 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 74 <url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> 75 </urlset> 76 """ % date.today().strftime('%Y-%m-%d')) 77 37 78 @skipUnless(settings.USE_I18N, "Internationalization is not enabled") 38 79 def test_localized_priority(self): 39 80 "The priority value should not be localized (Refs #14164)" -
django/contrib/sitemaps/tests/urls.py
27 27 } 28 28 29 29 urlpatterns = patterns('django.contrib.sitemaps.views', 30 (r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}), 31 (r'^simple/custom-index\.xml$', 'index', {'sitemaps': simple_sitemaps, 'template_name': u'custom_sitemap_index.xml'}), 32 (r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), 30 33 (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), 34 (r'^simple/custom-sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps, 'template_name': u'custom_sitemap.xml'}), 31 35 (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}), 32 36 (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}), 33 37 ) -
django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- This is a customised template --> 3 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 4 {% for location in sitemaps %}<sitemap><loc>{{ location }}</loc></sitemap>{% endfor %} 5 </sitemapindex> -
django/contrib/sitemaps/tests/templates/custom_sitemap.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- This is a customised template --> 3 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 4 {% spaceless %} 5 {% for url in urlset %} 6 <url> 7 <loc>{{ url.location }}</loc> 8 {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %} 9 {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %} 10 {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %} 11 </url> 12 {% endfor %} 13 {% endspaceless %} 14 </urlset> -
django/contrib/sitemaps/views.py
5 5 from django.utils.encoding import smart_str 6 6 from django.core.paginator import EmptyPage, PageNotAnInteger 7 7 8 def index(request, sitemaps ):8 def index(request, sitemaps, template_name='sitemap_index.xml'): 9 9 current_site = get_current_site(request) 10 10 sites = [] 11 11 protocol = request.is_secure() and 'https' or 'http' … … 20 20 if pages > 1: 21 21 for page in range(2, pages+1): 22 22 sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) 23 xml = loader.render_to_string( 'sitemap_index.xml', {'sitemaps': sites})23 xml = loader.render_to_string(template_name, {'sitemaps': sites}) 24 24 return HttpResponse(xml, mimetype='application/xml') 25 25 26 def sitemap(request, sitemaps, section=None):26 def sitemap(request, sitemaps, template_name='sitemap.xml', section=None): 27 27 maps, urls = [], [] 28 28 if section is not None: 29 29 if section not in sitemaps: … … 43 43 raise Http404("Page %s empty" % page) 44 44 except PageNotAnInteger: 45 45 raise Http404("No page '%s'" % page) 46 xml = smart_str(loader.render_to_string( 'sitemap.xml', {'urlset': urls}))46 xml = smart_str(loader.render_to_string(template_name, {'urlset': urls})) 47 47 return HttpResponse(xml, mimetype='application/xml') -
docs/ref/contrib/sitemaps.txt
291 291 URLs. In this case, Django will automatically paginate the sitemap, and the 292 292 index will reflect that. 293 293 294 Template customization 295 ====================== 296 297 If you wish to use a different template for each sitemap or sitemap index available on your site, 298 you may specify it by passing a `template_name` parameter to the `sitemap` and `index` views via 299 the URLconf: 300 301 (r'^custom-sitemap\.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps, 'template_name': u'custom_sitemap.html'}), 302 (r'^custom-sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps, 'template_name': u'custom_sitemap.html'}), 303 294 304 Pinging Google 295 305 ============== 296 306