use sitemap
This commit is contained in:
parent
310cf4659d
commit
12b4bc1fbd
1 changed files with 45 additions and 7 deletions
52
app/views.py
52
app/views.py
|
@ -1,3 +1,6 @@
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
from django.utils.timezone import datetime, timedelta
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
|
@ -5,17 +8,52 @@ from django.http import HttpResponse
|
||||||
def robots_txt(request):
|
def robots_txt(request):
|
||||||
txt = '''User-agent: *
|
txt = '''User-agent: *
|
||||||
Disallow:
|
Disallow:
|
||||||
'''
|
|
||||||
return HttpResponse(txt, 'text/plain')
|
|
||||||
|
|
||||||
txt = '''User-agent: *
|
|
||||||
Disallow:
|
|
||||||
Sitemap: {}
|
Sitemap: {}
|
||||||
'''.format(request.build_absolute_uri('/sitemap.xml'))
|
'''.format(request.build_absolute_uri('/sitemap.xml'))
|
||||||
return HttpResponse(txt, 'text/plain')
|
return HttpResponse(txt, 'text/plain')
|
||||||
|
|
||||||
|
|
||||||
def sitemap_xml(request):
|
def sitemap_xml(request):
|
||||||
sitemap = ''
|
now = datetime.now()
|
||||||
return HttpResponse(sitemap, 'application/xml')
|
from .item.models import Item
|
||||||
|
urlset = ET.Element('urlset')
|
||||||
|
urlset.attrib['xmlns'] = "http://www.sitemaps.org/schemas/sitemap/0.9"
|
||||||
|
urlset.attrib['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
urlset.attrib['xsi:schemaLocation'] = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
|
||||||
|
|
||||||
|
url = ET.SubElement(urlset, "url")
|
||||||
|
loc = ET.SubElement(url, "loc")
|
||||||
|
loc.text = request.build_absolute_uri('/')
|
||||||
|
lastmod = ET.SubElement(url, "lastmod")
|
||||||
|
lastmod.text = now.strftime("%Y-%m-%d")
|
||||||
|
changefreq = ET.SubElement(url, "changefreq")
|
||||||
|
changefreq.text = 'hourly'
|
||||||
|
priority = ET.SubElement(url, "priority")
|
||||||
|
priority.text = '1.0'
|
||||||
|
|
||||||
|
url = ET.SubElement(urlset, "url")
|
||||||
|
loc = ET.SubElement(url, "loc")
|
||||||
|
loc.text = request.build_absolute_uri('/archive/')
|
||||||
|
lastmod = ET.SubElement(url, "lastmod")
|
||||||
|
lastmod.text = now.strftime("%Y-%m-%d")
|
||||||
|
changefreq = ET.SubElement(url, "changefreq")
|
||||||
|
changefreq.text = 'weekly'
|
||||||
|
priority = ET.SubElement(url, "priority")
|
||||||
|
priority.text = '1.0'
|
||||||
|
|
||||||
|
for item in Item.objects.exclude(published=None).exclude(published__gt=now).order_by('-published'):
|
||||||
|
url = ET.SubElement(urlset, "url")
|
||||||
|
loc = ET.SubElement(url, "loc")
|
||||||
|
loc.text = request.build_absolute_uri(item.get_absolute_url())
|
||||||
|
# This date should be in W3C Datetime format, can be %Y-%m-%d
|
||||||
|
lastmod = ET.SubElement(url, "lastmod")
|
||||||
|
lastmod.text = item.modified.strftime("%Y-%m-%d")
|
||||||
|
# always, hourly, daily, weekly, monthly, yearly, never
|
||||||
|
changefreq = ET.SubElement(url, "changefreq")
|
||||||
|
changefreq.text = 'daily'
|
||||||
|
# priority of page on site values 0.1 - 1.0
|
||||||
|
priority = ET.SubElement(url, "priority")
|
||||||
|
priority.text = '0.9'
|
||||||
|
data = b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset)
|
||||||
|
return HttpResponse(data, 'application/xml')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue