长篇大论的道歉。我正在尝试为一些带有一些静态页面的基本django博客应用程序生成sitemap.xml。我不确定为什么会出错。注意:我已经创建了一个虚拟博客帖子,当我收到错误时它就存在了。
我试着查看django文档,但发现它太混乱了。在线教程也没什么帮助。
**Error:**
NoReverseMatch at /sitemap.xml
Reverse for 'post_detail' with no arguments not found. 1 pattern(s) tried:
['post/(?P<pk>\\d+)$']
**sitemap.py**
from django.contrib.sitemaps import Sitemap
from blog.models import Post
from blog.urls import urlpatterns as homeUrls
from django.core.urlresolvers import reverse
class PostSitemap(Sitemap):
changefreq = "weekly"
priority = 0.5
def items(self):
return Post.objects.all()
class StaticSitemap(Sitemap):
priority = 0.8
changefreq = 'weekly'
# The below method returns all urls defined in urls.py file
def items(self):
mylist = [ ]
for url in homeUrls:
if url.name is None:
pass;
else:
mylist.append(''+str(url.name))
return mylist
def location(self, item):
return reverse(item)
**models.py**
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def get_absolute_url(self):
return reverse("post_detail",kwargs={'pk':self.pk})
**urls.py**
...
from django.contrib.sitemaps.views import sitemap
from .sitemaps import *
sitemaps = {
'posts': PostSitemap(),
'static': StaticSitemap(),
}
urlpatterns = [
...
url(r'^sitemap\.xml$', sitemap, {'sitemaps':
sitemaps},name='django.contrib.sitemaps.views.sitemap'),
url(r'^post/(?P<pk>\d+)$', views.PostDetailView.as_view(),
name='post_detail'),
...
]
**settings.py**
INSTALLED_APPS - [
...
'django.contrib.sitemaps',
'django.contrib.sites',
'robots',
...
]
I am very curious to understand how I can solve this error.
Thanks a lot !
EDIT1;添加在postdetail的网址中。