Django站点地图:仅允许在文档开头使用XML声明
我一直致力于在Django-2.2上为博客网站实现站点地图 我遵循的代码结构是- Sitemaps.py url.py 设置.py 我想这差不多是因为我引用了这么多链接。 但是当我打开Django站点地图:仅允许在文档开头使用XML声明,django,python-3.x,sitemap,django-2.2,django-sitemaps,Django,Python 3.x,Sitemap,Django 2.2,Django Sitemaps,我一直致力于在Django-2.2上为博客网站实现站点地图 我遵循的代码结构是- Sitemaps.py url.py 设置.py 我想这差不多是因为我引用了这么多链接。 但是当我打开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 d
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.
就这样,服务器日志上没有任何内容。
请,如果有人能帮我的话。
提前感谢您的xml文档开头有新行。这是您收到此问题的主要原因 请根据文档更改URL.py文件 您的url应该如下所示
from django.contrib.sitemaps.views import sitemap
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')
您的模板中是否有
sitemap.xml
文件?另外,我不喜欢URL^sitemap\.xml/$
末尾的/
,为什么不像Django的例子那样使用路径
?不,我没有任何名为sitemap.xml
的文件。好吧,我将删除尾随的/
,但我想这与解决方案无关。是的,这是额外的建议。错误表明在XML声明行上方有一些内容,这对于某些浏览器是不正确的(使用curl检查并查看响应的第一行中有什么内容)。如果您使用的是Django的默认模板,那么应该有一个中间件或其他东西添加到响应的开头。可能其他第三方软件包有一个带有sitemap.xml
name.Followed的模板-我收到了相同的错误&curl命令-127.0.0.1:8000
INSTALLED_APPS = [
...
'django.contrib.sites',
'django.contrib.sitemaps',
]
SITE_ID = 1
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.
from django.contrib.sitemaps.views import sitemap
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')