我一直致力于在 Django-2.2 上为博客网站实施站点地图。
我遵循的代码结构是-
站点地图.py
from django.contrib.sitemaps import Sitemap
from .models import Post
class PostSitemap(Sitemap):
changefreq = "never"
priority = 0.9
def items(self):
return Post.objects.all()
网址.py
from django.contrib.sitemaps.views import sitemap
from .sitemaps import PostSitemap
sitemaps = {
'posts': PostSitemap
}
urlpatterns = [
url(r'^sitemap\.xml/$', sitemap, {'sitemaps' : sitemaps } , name='sitemap'),
]
设置.py
INSTALLED_APPS = [
...
'django.contrib.sites',
'django.contrib.sitemaps',
]
SITE_ID = 1
我想这几乎就是它,因为我引用了这么多链接。但是当我打开127.0.0.1:8000/sitemap.xml
它时,它给了我以下错误-
This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
就是这样,服务器日志上没有任何内容。请,如果有人可以请帮助我。提前致谢