'Why isn't Django Sitemap showing the URLs
I've been trying to make my django sitemaps work for a small blog application.
After checking out multiple tutorials, I still can't get the sitemaps work properly.
Here's my urls.py:
from django.contrib import admin, sitemaps
from django.urls import path
from django.urls import include
from rx import views
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.sitemaps.views import sitemap
from rx.sitemaps import Static_Sitemap
sitemaps = {
'static': Static_Sitemap,
}
urlpatterns = [
path('ckeditor/', include('ckeditor_uploader.urls')),
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('blog/', views.blog, name='blog'),
path('about/', views.about, name='about'),
path('<slug:cat_slug_name>/', views.cat, name='cat'),
path('<slug:cat_slug_name>/<slug:post_slug_name>/', views.blog_post, name='blog_post'),
path('sitemap.xml/', sitemap, {'sitemaps': sitemaps}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here's my settings.py:
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
MEDIA_DIR = os.path.join(BASE_DIR, 'media')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rx',
'ckeditor',
'ckeditor_uploader',
'django.contrib.sitemaps',
'django.contrib.sites',
]
SITE_ID = 1
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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 = 'rankxero.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR, ],
'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 = 'rankxero.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
MEDIA_URL = '/media/'
MEDIA_ROOT = MEDIA_DIR
CKEDITOR_UPLOAD_PATH = 'uploads/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Here's the Models.py:
from django.db import models
from django.template.defaultfilters import slugify
from django.utils.timezone import now
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
from django.urls import reverse
class Category(models.Model):
title = models.CharField(max_length=128, unique=True)
subtitle = models.CharField(max_length=512, unique=True)
slug = models.SlugField(unique=True)
posts_in_category = models.CharField(max_length=128, default=0)
meta_description = models.CharField(max_length=160, blank=True)
sitemap_url = models.CharField(max_length=128, blank=True)
class Meta:
verbose_name_plural = '1. Categories'
def __str__(self):
return self.title
class Post(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
post_name = models.CharField(max_length=128, unique=True)
slug = models.SlugField(unique=True, blank=True, null=True)
featured_image = models.ImageField(blank=True)
content = RichTextUploadingField(default="Write the beginning of your article here")
author = models.CharField(max_length=15, default="Salman")
date = models.DateTimeField(default=now)
date_modified = models.DateTimeField(default=now)
reading_time = models.CharField(max_length=128, blank=True)
meta_description = models.CharField(max_length=200, blank=True, null=True)
sitemap_url = models.CharField(max_length=128, blank=True)
class Meta:
verbose_name_plural = '2. Posts'
def __str__(self):
return self.post_name
def get_absolute_url(self):
return f'{self.category}/{self.slug}/'
class About(models.Model):
featured_image = models.ImageField(blank=True)
name = models.CharField(max_length=128)
content = RichTextField(default="Write the beginning of your article here")
slug = models.SlugField(unique=True, blank=True, null=True)
class Meta:
verbose_name_plural = '3. About'
def __str__(self):
return self.name
def get_absolute_url(self):
return f'/{self.slug}'
Here's the sitemaps.py file:
from django.contrib import sitemaps
from django.urls import reverse
from django.contrib.sitemaps import Sitemap
class Static_Sitemap(Sitemap):
priority = 1.0
changefreq = 'yearly'
def items(self):
return ['index', 'about', 'blog']
def location(self, item):
return reverse(item)
I am only trying to add the static pages to the sitemap. Haven't started with the dynamic pages.
The sitemap does show results but this is what I get:
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/</loc>
<lastmod>2004-10-01T18:23:17+00:00</lastmod>
</sitemap>
<sitemap>
<loc>http://www.example.com/sitemap2.xml.gz</loc>
<lastmod>2005-01-01</lastmod>
</sitemap>
</sitemapindex>
Solution 1:[1]
sometimes the URLs are not indexed or there might be conflicts. Adding name will clear this can you try adding name in urls
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')
Solution 2:[2]
I was having the same problem however the solution for me was a little different so I thought I would post.
I had to change the path to 'sitemap'
path('sitemap', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Suhas Kashyap |
Solution 2 | Cam |