【问题标题】:Django get_absolute_url on sitemap.xml getting NoReverMatch errorsitemap.xml 上的 Django get_absolute_url 出现 NoReverMatch 错误
【发布时间】:2015-12-30 04:16:11
【问题描述】:

models.py

def get_absolute_url(self):
    return reverse('abc', kwargs={'slug': self.slug})

urls.py

from posts.views import xyz
from posts.sitemaps import PostSitemap


sitemaps = {
    'posts': PostSitemap()
}

urlpatterns = patterns(
    url(r'^posts/(?P<slug>[\w-]+)/$',xyz, name='abc'),
    url(r'^sitemap\.xml$',sitemap, {'sitemaps': sitemaps}),
)

站点地图.py

class PostSitemap(Sitemap):
    changefreq = 'daily'
    pirority = 0.5

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

我没有更改默认的 contib/sitemaps/templates/sitemap.xml,当我浏览 mysite/sitemap.xml 时它会抛出错误:

没有找到带有参数“()”和关键字参数“{'slug': u'my-slug-goes-here'}”的“abc”。尝试了 0 个模式:[]

【问题讨论】:

  • 您是否尝试过从 shell 手动反转 url?像 reverse('abc', kwargs={'slug': 'singleword'})。正则表达式可能无法处理您的 slug 中的某些字符?
  • 嗨 Paulo,mysite/posts/my-slug-goes-here 之类的东西在我的模板上运行良好,只是 sitemap.xml 不工作,我会试试你说的。谢谢!
  • 我通过将其更改为 reverse('post:abc', kwargs={'slug': self.slug}) 以某种方式解决了它

标签: django sitemap mezzanine


【解决方案1】:
class PostSitemap(Sitemap):
    changefreq = 'daily'
    pirority = 0.5

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

    def location(self, item):
        return item.get_absolute_url()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多