Django 站点地图在 Heroku 上生成双 https://
Django sitemap generates with double https:// when on Heroku
我正在使用站点地图框架生成站点地图。 Google 搜索控制台指出站点地图是一种无效的文件格式。它似乎是有效的 XML 但站点地图中的 URL 有一个双 https://
,我不明白为什么。
只有在 Heroku 上托管并且 settings.py
中允许的主机设置为 Heroku 域名时才会发生这种情况。当我 运行 本地应用程序没有允许的主机时,站点地图会完美生成。
是什么原因造成的,我该如何解决?
站点地图:
<URL>
<loc>https://https://swflreliefrealty.herokuapp.com/</loc>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<URL>
<loc>https://https://swflreliefrealty.herokuapp.com/contact/</loc>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
Django sitemap.py
:
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from blog.models import Post
class StaticViewSitemap(Sitemap):
changefreq = 'daily'
priority = '0.5'
def items(self):
# Return list of url names for view to include in sitemap
return ['home', 'contact', 'success', 'blog']
def location(self, item):
return reverse(item)
class BlogSitemap(Sitemap):
changefreq = 'daily'
priority = '0.5'
def items(self):
return Post.objects.all()
Django settings.py
:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG_VALUE')
ALLOWED_HOSTS = ['swflreliefrealty.herokuapp.com']
# Application definition
INSTALLED_APPS = [
'sendemail.apps.SendemailConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django.contrib.sites',
'robots',
'blog'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
我怀疑您的 sites.Site
模型在您的生产数据库中将其 domain
设置为 https://swflreliefrealty.herokuapp.com
。站点框架 is used by the sitemap framework.
站点的 domain
should just be set to the domain,没有协议,例如swflreliefrealty.herokuapp.com
.
由于它存储在您的数据库中,因此它很容易在生产(在 Heroku 上,它不起作用)和开发(在本地,它起作用的地方)中具有不同的价值。
我正在使用站点地图框架生成站点地图。 Google 搜索控制台指出站点地图是一种无效的文件格式。它似乎是有效的 XML 但站点地图中的 URL 有一个双 https://
,我不明白为什么。
只有在 Heroku 上托管并且 settings.py
中允许的主机设置为 Heroku 域名时才会发生这种情况。当我 运行 本地应用程序没有允许的主机时,站点地图会完美生成。
是什么原因造成的,我该如何解决?
站点地图:
<URL>
<loc>https://https://swflreliefrealty.herokuapp.com/</loc>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<URL>
<loc>https://https://swflreliefrealty.herokuapp.com/contact/</loc>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
Django sitemap.py
:
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from blog.models import Post
class StaticViewSitemap(Sitemap):
changefreq = 'daily'
priority = '0.5'
def items(self):
# Return list of url names for view to include in sitemap
return ['home', 'contact', 'success', 'blog']
def location(self, item):
return reverse(item)
class BlogSitemap(Sitemap):
changefreq = 'daily'
priority = '0.5'
def items(self):
return Post.objects.all()
Django settings.py
:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG_VALUE')
ALLOWED_HOSTS = ['swflreliefrealty.herokuapp.com']
# Application definition
INSTALLED_APPS = [
'sendemail.apps.SendemailConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django.contrib.sites',
'robots',
'blog'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
我怀疑您的 sites.Site
模型在您的生产数据库中将其 domain
设置为 https://swflreliefrealty.herokuapp.com
。站点框架 is used by the sitemap framework.
站点的 domain
should just be set to the domain,没有协议,例如swflreliefrealty.herokuapp.com
.
由于它存储在您的数据库中,因此它很容易在生产(在 Heroku 上,它不起作用)和开发(在本地,它起作用的地方)中具有不同的价值。