Creating a sitemap class returning URL with two arguments

Viewed 37

I have a set of items = [a,b,c]

And I have a page that takes in two items and compare them.

So the URLs look like this:

http://127.0.0.1:8000/compare/a/b

http://127.0.0.1:8000/compare/a/c

http://127.0.0.1:8000/compare/b/c

How could I make a sitemap class to represent all of them?

*This is my sitemap class with one argument

class ItemsSitemap(Sitemap):
    changefreq = "monthly"
    priority = 0.8

    def items(self):
        return Items.objects.order_by('id')

    def location(self, obj):
        return '/items/%s' % (obj.id)
1 Answers

Okay, so this is how I got it to work:

from itertools import combinations    

class ComparisonSitemap(Sitemap):
    changefreq = "monthly"
    priority = 0.8

    def items(self):
        itemlist= Items.objects.order_by('id')
        itemids= itemlist.values_list('id',flat=True)
        pairs = list(combinations(itemids,2))
        return pairs

    def location(self,pairs):
        return '/comparison/{}/{}'.format(pairs[0],pairs[1])

Thank you Iain for the tip!

Related