Django站点地图在Heroku上使用双https://生成

2024-04-29 12:23:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用站点地图框架生成站点地图。Google搜索控制台声明站点地图是无效的文件格式。它似乎是有效的XML,但站点地图中的URL有一个双https://,我不知道为什么

只有在Heroku上托管并且settings.py中允许的主机设置为Heroku域名时,才会发生这种情况。当我在没有允许的主机的情况下在本地运行应用程序时,sitemap会生成完美的结果

这是什么原因造成的,我如何修复它

网站地图:

<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>

Djangositemap.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()

Djangosettings.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'

Tags: pathdjangohttpscom站点oscontext地图
1条回答
网友
1楼 · 发布于 2024-04-29 12:23:21

我怀疑您的sites.Site模型在生产数据库中的domain设置为https://swflreliefrealty.herokuapp.com。网站框架is used by the sitemap framework

站点的domain{a2},没有协议,例如swflreliefrealty.herokuapp.com

因为它存储在数据库中,所以很容易在生产(在Heroku上,它不起作用)和开发(在本地,它起作用)中有不同的价值

相关问题 更多 >