mod: современный способ создания sitemap.xml

This commit is contained in:
2026-02-18 16:00:52 +03:00
parent 33fa2d04a9
commit 65feb36f77
7 changed files with 68 additions and 29 deletions

View File

@@ -52,6 +52,8 @@ INSTALLED_APPS: list[str] = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.sitemaps',
'taggit.apps.TaggitAppConfig', 'taggit.apps.TaggitAppConfig',
'web.apps.WebConfig', 'web.apps.WebConfig',
] ]
@@ -138,3 +140,5 @@ STATICFILES_DIRS = [
# Enable WhiteNoise's Gzip compression of static assets. # Enable WhiteNoise's Gzip compression of static assets.
if not DEBUG: if not DEBUG:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
SITE_ID = 1

View File

@@ -17,15 +17,21 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.urls import path, re_path from django.urls import path, re_path
from django.conf.urls.static import static from django.conf.urls.static import static
from django.contrib.sitemaps.views import sitemap
from dicquo import settings from dicquo import settings
from web import views from web import views
from web.sitemaps import DictumSitemap
sitemaps = {
'dictums': DictumSitemap,
}
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
re_path(r'^$', views.index), re_path(r'^$', views.index),
re_path(r'^(?P<dq_id>\d{1,12})_\S*$', views.by_id), re_path(r'^(?P<dq_id>\d{1,12})_\S*$', views.by_id),
re_path(r'^sitemap.xml$', views.sitemap), path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
] ]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>https://dq.cube2.ru/</loc><priority>0.1</priority></url>{% for I in DATA %}
<url><loc>https://dq.cube2.ru/{{ I.ID }}_{{ I.SLUG }}</loc><priority>1</priority></url>{% endfor %}
</urlset>

View File

@@ -0,0 +1,18 @@
# Generated by Django 6.0.2 on 2026-02-18 12:21
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('web', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='tbdictumandquotes',
name='bIsChecked',
field=models.BooleanField(db_index=True, default=True, help_text='Цитата проверена.', verbose_name='Проверен'),
),
]

View File

@@ -243,22 +243,22 @@ class TbAuthor(models.Model):
return self.__str__() return self.__str__()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
http = urllib3.PoolManager() # http = urllib3.PoolManager()
# последовательно # последовательно
# Используем типограф typus (https://github.com/byashimov/typus) # Используем типограф typus (https://github.com/byashimov/typus)
# Используем типограф Eugene Spearance (http://www.typograf.ru/) # Используем типограф Eugene Spearance (http://www.typograf.ru/)
# Используем типограф Муравьева (http://mdash.ru/api.v1.php) # Используем типограф Муравьева (http://mdash.ru/api.v1.php)
self.szAuthor = ru_typus(self.szAuthor) self.szAuthor = ru_typus(self.szAuthor)
resp = http.request("POST", # resp = http.request("POST",
"http://www.typograf.ru/webservice/", # "http://www.typograf.ru/webservice/",
fields={"text": self.szAuthor.encode('cp1251')}) # fields={"text": self.szAuthor.encode('cp1251')})
self.szAuthorHTML = resp.data.decode('cp1251') # self.szAuthorHTML = resp.data.decode('cp1251')
# print(self.szContentHTML) # # print(self.szContentHTML)
resp = http.request("POST", # resp = http.request("POST",
"http://mdash.ru/api.v1.php", # "http://mdash.ru/api.v1.php",
fields={"text": self.szAuthorHTML.encode('utf-8')}) # fields={"text": self.szAuthorHTML.encode('utf-8')})
self.szAuthorHTML = json.loads(resp.data)["result"] # self.szAuthorHTML = json.loads(resp.data)["result"]
# print(self.szContentHTML) # # print(self.szContentHTML)
super(TbAuthor, self).save(*args, **kwargs) super(TbAuthor, self).save(*args, **kwargs)
class Meta: class Meta:
@@ -315,6 +315,12 @@ class TbDictumAndQuotes(models.Model):
verbose_name=u"Типографить", verbose_name=u"Типографить",
help_text=u"Применять типографику?" help_text=u"Применять типографику?"
) )
bIsChecked = models.BooleanField(
default=True,
db_index=True,
verbose_name=u"Проверен",
help_text=u"Цитата проверена."
)
kAuthor = models.ForeignKey( kAuthor = models.ForeignKey(
TbAuthor, TbAuthor,
default=None, default=None,

20
dicquo/web/sitemaps.py Normal file
View File

@@ -0,0 +1,20 @@
from django.contrib.sitemaps import Sitemap
from web.models import TbDictumAndQuotes
import pytils
class DictumSitemap(Sitemap):
changefreq = "weekly" # Как часто меняются страницы
priority = 0.9 # Приоритет (от 0.0 до 1.0)
def items(self):
# Only show checked items in sitemap
return TbDictumAndQuotes.objects.filter(bIsChecked=True).order_by('-id')
def lastmod(self, obj):
return obj.dtEdited
def location(self, obj):
# Generates URL in format: /123_slug
slug = pytils.translit.slugify(obj.szContent.lower())[:120]
return f"/{obj.id}_{slug}"

View File

@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__author__ = "Sergei Erjemin" __author__ = "Sergei Erjemin"
__copyright__ = "Copyright 2020, Sergei Erjemin" __copyright__ = "Copyright 2020-2026, Sergei Erjemin"
__credits__ = ["Sergei Erjemin", ] __credits__ = ["Sergei Erjemin", ]
__license__ = "GPL" __license__ = "GPL"
__version__ = "0.0.1" __version__ = "0.3.9"
__maintainer__ = "Sergei Erjemin" __maintainer__ = "Sergei Erjemin"
__email__ = "erjemin@gmail.com" __email__ = "erjemin@gmail.com"
__status__ = "in progress" __status__ = "in progress"
@@ -92,13 +92,3 @@ def index(request):
response = render(request, template, to_template) response = render(request, template, to_template)
return response return response
def sitemap(request):
template = "sitemap.xml" # шаблон
to_template = []
dq = TbDictumAndQuotes.objects.order_by('id').all()
for i in dq:
to_template.append({"ID": i.id,
"SLUG": pytils.translit.slugify(i.szContent.lower()[:120])})
response = render(request, template, {"DATA": to_template})
return response