Почему Django Sitemap не показывает URL-адреса
Я пытаюсь заставить мои ситемы django работать для небольшого приложения для блога.
После проверки нескольких руководств, я все еще не могу заставить sitemaps работать должным образом.
Вот мой 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)
Вот мой settings.py:
Вот 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}'
Вот файл sitemaps.py:
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)
Я пытаюсь добавить в карту сайта только статические страницы. К динамическим страницам еще не приступал.
Карта сайта действительно показывает результаты, но вот что я получаю:
<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>
Иногда URL не индексируются или могут возникать конфликты. Добавление имени устранит это Вы можете попробовать добавить имя в urls
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')