diff --git a/content/templates/jinja2/sitemap.html b/content/templates/jinja2/sitemap.html index 693438e..305ba33 100644 --- a/content/templates/jinja2/sitemap.html +++ b/content/templates/jinja2/sitemap.html @@ -1,16 +1,14 @@ - - - - {% for p in object.pages_slugs %} - - {{ object.domain }}/pages/{{ p }}.html - + +{% for url in urlset %} + + {{ url.location }} + {% if url.lastmod %}{{ url.lastmod.strftime(url.item.lastmod_format) }}{% endif %} + {% if url.changefreq %}{{ url.changefreq }}{% endif %} + {% if url.priority %}{{ url.priority }}{% endif %} + {% for alternate in url.alternates %} + {% endfor %} - {% for p in object.posts_slugs %} - - {{ object.domain }}/posts/{{ p }}.html - - {% endfor %} - - \ No newline at end of file + +{% endfor %} + diff --git a/jssg/models.py b/jssg/models.py index eeb83fe..65c9fe3 100644 --- a/jssg/models.py +++ b/jssg/models.py @@ -226,6 +226,7 @@ class Page(Document): """A webpage, with a title and some content.""" BASE_DIR = settings.JFME_PAGES_DIRS + lastmod_format = settings.JFME_SITEMAP_LASTMOD_DATETIME_FORMAT def __init__(self, content: str, **metadata) -> None: """Create a new page. diff --git a/jssg/settings.py b/jssg/settings.py index b6aae4b..840dd9a 100644 --- a/jssg/settings.py +++ b/jssg/settings.py @@ -53,6 +53,7 @@ JFME_TEMPLATES_DIRS = [path / "templates" for path in JFME_CONTENT_DIRS] JFME_STATIC_DIRS = [path / "static" for path in JFME_CONTENT_DIRS] JFME_NUMBER_OF_POSTS_BY_PAGE = 3 +JFME_SITEMAP_LASTMOD_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z" # %Y:YYYY, %m:MM, %d:DD, %z:+/-HHMM #Django sites and sitemap app SITE_ID = 1 diff --git a/jssg/sitemaps.py b/jssg/sitemaps.py index 9715649..ab625a6 100644 --- a/jssg/sitemaps.py +++ b/jssg/sitemaps.py @@ -3,8 +3,10 @@ from django.contrib.sites.models import Site from jssg.models import Page, Post, PostList from django.conf import settings +from datetime import datetime class MySitemap(Sitemap) : + #Overriding get_url() to specify the domain name def get_urls(self, site=None, **kwargs): site = Site(domain=settings.JFME_DOMAIN, name=settings.JFME_DOMAIN) return super(MySitemap, self).get_urls(site=site, **kwargs) @@ -23,6 +25,8 @@ def location(self, page) -> str: return "/" + page.rel_folder_path + "/" + page.slug + ".html" else : return "/" + page.slug + ".html" + def lastmod(self, post) : + return datetime.fromtimestamp(post.path.lstat().st_mtime) class PostSitemap(MySitemap) : def items(self) : @@ -32,6 +36,8 @@ def location(self, post) -> str: return "/posts/article/" + post.rel_folder_path + "/" + post.slug + ".html" else : return "/posts/articles/" + post.slug + ".html" + def lastmod(self, post) : + return datetime.fromtimestamp(post.path.lstat().st_mtime) class PostListSitemap(MySitemap) : def items(self) : diff --git a/jssg/urls.py b/jssg/urls.py index 8954378..bf24ef7 100644 --- a/jssg/urls.py +++ b/jssg/urls.py @@ -70,7 +70,10 @@ distill_path( "sitemap.xml", sitemap, - {"sitemaps": {"constant-url": ConstantUrlSitemap, "page": PageSitemap, "posts": PostSitemap, "postlist":PostListSitemap}}, + { + "sitemaps": {"constant-url": ConstantUrlSitemap, "page": PageSitemap, "posts": PostSitemap, "postlist":PostListSitemap}, + "template_name":"sitemap.html" + }, name="django.contrib.sitemaps.views.sitemap", ) ]