Add only URLs having records to sitemap
I'm trying to add a few custom URLs, all linked to one view, with the parameters passed being different. As an example, I'll be having several URLs to pull up all records having a certain make, so it'll be <root_url>/vehicles/inventory/<make>
. What I want to do if to ensure that the URLs actually return some records...only those that do will need to go to the sitemap.
Here's how I've done the class for all makes in a certain year (but some of the URLs don't have any vehicles being returned and I want to eliminate that).
class YearMakeSitemap(Sitemap):
priority = 1.0
changefreq = 'weekly'
def items(self):
makes = query.order_by('manufacturer_popularity', 'manufacturer').values_list
('manufacturer', flat = True)
years = query.order_by('year').values_list('year', flat = True)
url = []
for make in makes:
for year in years:
ur开发者_如何学Gol.append('%s/%s' % (make, year))
return url
def lastmod(self, obj):
return datetime.now()
def location(self, obj):
return '/vehicles/inventory/%s/' % obj
How can have the sitemap file having only the URLs that return at least one record as opposed to my current situation?
How about making this change?
for make in makes:
for year in years:
if query.filter(year=year,manufacturer=make).count() > 0:
url.append('%s/%s' % (make, year))
But I think a more efficient answer would be to do this:
query.order_by("manufacturer_popularity","manufacturer","year").values("manufacturer","year").distinct()
and iterate over that list.
精彩评论