首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django sitemaps: XML声明仅允许在文档开始时使用

Django sitemaps: XML声明仅允许在文档开始时使用
EN

Stack Overflow用户
提问于 2019-10-14 10:02:52
回答 1查看 498关注 0票数 1

我一直在致力于为博客网站在Django-2.2上实现站点地图。

我遵循的代码结构是-

Sitemaps.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    'django.contrib.sitemaps',
]

SITE_ID = 1

我想这差不多就是我引用了那么多链接。但是当我打开127.0.0.1:8000/sitemap.xml时,它会给我一个错误-

代码语言:javascript
复制
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.

就这样,服务器日志上什么都没有。如果有人能帮帮我的话。提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-14 11:32:57

xml文档的开头有新行。这是你得到这个问题的主要原因。

请根据文档更改您的urls.py文件。

https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/

你的网址应该如下所示。

代码语言:javascript
复制
from django.contrib.sitemaps.views import sitemap

path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
     name='django.contrib.sitemaps.views.sitemap')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58374416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档