我一直在致力于为博客网站在Django-2.2上实现站点地图。
我遵循的代码结构是-
Sitemaps.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()urls.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'),
]settings.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.就这样,服务器日志上什么都没有。如果有人能帮帮我的话。提前感谢
发布于 2019-10-14 11:32:57
xml文档的开头有新行。这是你得到这个问题的主要原因。
请根据文档更改您的urls.py文件。
https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/
你的网址应该如下所示。
from django.contrib.sitemaps.views import sitemap
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')https://stackoverflow.com/questions/58374416
复制相似问题