Hello, I’ve been following the documentation to add a sitemap to my website, everything works perfectly on development, once I upload to production, I have 404 errors as in sitemap can’t be found. I checked the database on production to make sure the SITE_ID is the same as the pk of the site registered in database.
This is my installed app in settings.py
"""
Django settings for icerd project.
Generated by 'django-admin startproject' using Django 4.0.6.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from pathlib import Path
from django.conf import global_settings
# Add custom languages not provided by Django
from django.conf import locale
from django.utils.translation import gettext_lazy as _
from icerd.productions import production_debug, production_secret_key, allowed_host, production_caches_location
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = production_debug
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = production_secret_key
ALLOWED_HOSTS = ["*"] if DEBUG else allowed_host
AUTH_USER_MODEL = "registration.User"
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'homepage.apps.HomepageConfig',
'administration.apps.AdministrationConfig',
'registration.apps.RegistrationConfig',
]
# Multisite settings
SITE_ID = 1
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
# DataFlair #Caching Middleware
# 'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.cache.FetchFromCacheMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'registration.middleware.ActiveUserMiddleware',
]
ROOT_URLCONF = 'icerd.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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 = 'icerd.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
if DEBUG:
STATICFILES_DIRS = [
BASE_DIR / "static",
]
if not DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = 'static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Message storage
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
# Number of seconds of inactivity before a user is marked offline
USER_ONLINE_TIMEOUT = 300
# Number of seconds that we will keep track of inactive users for before
# their last seen is removed from the cache
USER_LASTSEEN_TIMEOUT = 60 * 60 * 24 * 7
# Caches settings
CACHES_LOCATION = production_caches_location
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': CACHES_LOCATION,
}
}
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '*********'
EMAIL_HOST_PASSWORD = '********'
EMAIL_USE_TLS = True
EMAIL_SUBJECT_PREFIX = ""
EMAIL_USE_LOCALTIME = True
ADMINS = [
('Admin', 'example@gmail.com'),
]
MANAGERS = [
('Admin', 'example@gmail.com'),
]
# Language translation settings
EXTRA_LANG_INFO = {
'cr-ht': {
'bidi': False, # right-to-left
'code': 'cr-ht',
'name': 'Haitian Creole',
'name_local': "Kreyòl",
},
}
LANG_INFO = dict(locale.LANG_INFO, **EXTRA_LANG_INFO)
locale.LANG_INFO = LANG_INFO
LANGUAGES = (
('cr-ht', _('Kreyòl')),
('fr', _('Français')),
('en', _('English')),
)
# Languages using BiDi (right-to-left) layout
LANGUAGES_BIDI = global_settings.LANGUAGES_BIDI + ["cr-ht"]
LOCALE_PATHS = (
os.path.join(BASE_DIR / 'locale'),
)
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
LANGUAGE_CODE = 'fr'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Extra deployment parameters
if not DEBUG:
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 60
SECURE_HSTS_PRELOAD = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 900 # 15 minutes
LOGIN_URL = '/login/'
LOGOUT_REDIRECT_URL = LOGIN_URL
# The number of seconds a password reset link is valid for (in second)
PASSWORD_RESET_TIMEOUT = 3600 # 1 hour
and this is my urls.py (root project where settings.py file is)
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.contrib.sitemaps.views import sitemap
from django.urls import path, include
from icerd import settings
from icerd.sitemaps import StaticViewSitemap
admin.site.site_header = "ICERD' website Administration"
admin.site.site_title = 'Administration'
admin.site.index_title = 'ICERD'
sitemaps = {
'static': StaticViewSitemap,
}
urlpatterns = [
path('i18n/', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
path('', include('homepage.urls')),
path('registration/', include('registration.urls')),
path('administration/', include('administration.urls')),
path('reset_password/', auth_views.PasswordResetView.as_view(template_name="password/reset_password.html"),
name='reset_password'),
path('reset_password_sent/',
auth_views.PasswordResetDoneView.as_view(template_name="password/reset_password_sent.html"),
name='password_reset_done'),
path('reset/<uidb64>/<token>',
auth_views.PasswordResetConfirmView.as_view(template_name="password/reset_password_form.html"),
name='password_reset_confirm'),
path('reset_password_complete/',
auth_views.PasswordResetCompleteView.as_view(template_name="password/reset_password_done.html"),
name='password_reset_complete'),
]
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
and this is my sitemaps.py (also in root, where settings.py is)
# sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
class StaticViewSitemap(Sitemap):
priority = 0.5
changefreq = 'weekly'
def items(self):
return [
'homepage:homepage',
'homepage:get_logo',
'homepage:about_us',
'homepage:login',
'homepage:logout',
]
def location(self, item):
return reverse(item)
After all those, I made sure to run migrations, go to django admin and replace ‘example.com’ in sites’ table.
If I run the same code on my computer, everything works fine, production doesn’t work.
I use a debian vps to host my website using nginx and gunicorn.