仅将有记录的 URL 添加到站点地图
我正在尝试添加一些自定义 URL,所有这些 URL 都链接到一个视图,并且传递的参数不同。例如,我将使用多个 URL 来提取具有特定品牌的所有记录,因此它将是
。如果要确保 URL 实际上返回一些记录,我想要做什么...只有那些确实返回的记录才需要转到站点地图。
以下是我为某一年的所有品牌完成课程的方法(但有些 URL 没有返回任何车辆,我想消除它)。
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:
url.append('%s/%s' % (make, year))
return url
def lastmod(self, obj):
return datetime.now()
def location(self, obj):
return '/vehicles/inventory/%s/' % obj
与我当前的情况相反,如何让站点地图文件仅包含返回至少一条记录的 URL?
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:
url.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做出这样的改变怎么样?
但我认为更有效的答案是这样做:
query.order_by("manufacturer_popularity","manufacturer","year").values("manufacturer","year").distinct()
并迭代该列表。
How about making this change?
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.