Issues generating urls with HTTPS in sitemap of Django Project


(Immanuel) #1

I built a blog and I created a class inheriting from django.contrib.sitemaps.Sitemap like this:

from django.contrib.sitemaps import Sitemap
from .models import Post

class PostSitemap(Sitemap):
    changefreq = 'daily'
    priority = 0.9
    protocol = 'https'

    def items(self):
        return Post.published.all()

    def lastmod(self, obj):
        return obj.publish

Despite the fact that I assigned https to the protocol attribute, I keep getting links to blog posts with http protocol instead of https in my sitemap.xml. Here’s what I mean:

<item>
    <title>THE TITLE</title> 
    <link>http://www.shelteratyourcrossroads.com/2019/01/06/fluidity-grace/</link>
    <description>&lt;p&gt;Good morning Engraced One.&amp;nbsp;&lt;/p&gt; &lt;p&gt;It&amp;#39;s a beautiful day, I woke up this morning quite engrossed with my study of medicine and how difficult it is sometimes to appreciate visible progress, ...</description> 
    <guid>http://www.blahblahblah.com/YYYY/MM/DD/sluggity-slug/</guid>
</item> 

I want the urls in the <link> and <guid> tags to use the https protocol. How do I do that?

I created feeds for the blog posts like:

from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords
from .models import Post

class LatestPostsFeed(Feed):
    title = 'Some Title'
    link = 'https://www.blahblahblah.com/blog/'
    description = 'This is a blog about life. New posts every week'

    def items(self):
        return Post.published.all()[:7]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return truncatewords(item.body, 30)

(Immanuel) #2

@vitor please help. I can’t just don’t understand what’s going on


(Vitor Freitas) #3

Hi @Cimmanuel!

The Sitemap and Feed are two different things things

In a sitemap, if you set protocol = 'https' it will hardcode the protocol and it should work

However in the XML you are showing it is a feed and not a sitemap.xml.

In the Feed subclass (i.e., your LatestPostsFeed) you should set a method named item_link:

class LatestPostsFeed(Feed):
    # you class...

    def item_link(self, item):
        return 'https://example.com/%s' % reverse('post', kwargs={'pk': item.pk})

Otherwise it will default to the model’s get_absolute_url


(Immanuel) #4

Thank you very much @vitor!
I got it to work with the following:

    class LatestPostFeed(Feed):
        # your class...
    
        def item_link(self, item):
    	    return 'https://www.example.com{}'.format(
                reverse('post_detail', kwargs={
                    'year':item.publish.year, 
                    'month':item.publish.strftime('%m'), 
                    'day':item.publish.strftime('%d'), 
                    'post':item.slug}
                )
            )