How to manually create sitemap for django website
1. Use UTF-8
1.1. To check file use UTF-8 encoding $ file -bi <filename> // But if file contains only english characters it will show ascii although it is utf-8
2. If sitemap.xml it must have <urlset> <url> <loc>:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.outleys.site/sitemap/</loc>
</url>
<url>
<loc>http://www.outleys.site/sitemap/</loc>
</url>
</urlset>
3. If sitemap.txt just one link for one string. Must start with https://www.
4. Max size 50000 addresses or 50MB.
5. Add sitemap's url to robot.txt if you have it. Sitemap: http://www.outleys.site/sitemap.txt
6. To submit your sitemap to searchengine send http request with curl for ping tool <searchengine>/ping?sitemap=http://www.outleys.site/sitemap.xml And check response is 200
7. Place sitemap at the root.
8. How to place txt file with Django? Django has built-in functionality.
8.1. Edit settings.py:
INSTALLED_APPS += ['django.contrib.sites',
'django.contrib.sitemaps',]
8.2. In settings.py do not add SITE_ID = 1 // I am not sure it is necessary but it is switched translation off. It choose domain from Admin Panel -> Sites
8.3. Into url.py add:
from how_to.models import Article2
from django.contrib.sitemaps import GenericSitemap
from django.contrib.sitemaps.views import sitemap
info_dict = { 'queryset': Article2.objects.all(), }
urlpatterns += [
path('sitemap.xml', sitemap,
{'sitemaps': {'blog': GenericSitemap(info_dict, priority=0.6)}},
name='django.contrib.sitemaps.views.sitemap'),
]
8.4. When I am trying to go localhost:8000/sitemap.xml I got an error: Table .django_site doesn't exist
8.4.1. Do $ python manage.py makemigrations then $ python manage.py migrate
8.4.2. Now I get an error: '<model class name>' object has no attribute 'get_absolute_url'
8.4.3. In model class define method:
def get_absolute_url(self):
return reverse(''how_to:single_slug'', args=[ str(self.al.als.als)+"/"+str(self.al.al)+"/"+str(self.ali) ])
8.4.4. To change example com to my site go to Admin panel -> sites and change.
8.4.5. Sitemap doesn't show translated articles.
8.4.5.1. Into url.py try to override add parameter int sitemap class:
{'sitemaps': {'blog': GenericSitemap(info_dict, priority=0.6, i18n = True)}}, // Doesn't work
8.4.5.2. Try to move path variable under i18n_patterns:
urlpatterns = i18n_patterns(
path('sitemap.xml', sitemap,
{'sitemaps': {'blog': GenericSitemap(info_dict, priority=0.6,)}},
name='django.contrib.sitemaps.views.sitemap'),
) // Doesn't help. It just creates 3 sitemaps on 3 different urls. Like outleys.site/en/sitemap.xml and www.outleys.site/es/sitemap.xml
8.4.5.3. Override class django.contrib.sitemaps:
class BlogSitemap(GenericSitemap):
priority = 0.5
i18n = True
urlpatterns += path('sitemap.xml', sitemap,
{'sitemaps': {'blog': BlogSitemap(info_dict, )}},
name='django.contrib.sitemaps.views.sitemap'), // It works
8.4.6. Sitemap show unpublished articles.
8.4.6.1. Modify url.py it modifies database query like SELECT * FROM 'articles' WHERE 'published'='1' :
info_dict = { 'queryset': Article2.objects.filter(published="1"), }
8.4.6.2. If you have a model field inside of translations: translations__published="1" to access field.
8.4.7. After sitemap activation I loose article translations in admin edit panel.
8.4.7.1. Deactivate all changes in settings.py. // Delete SITE_ID=1.
8.5. When I type https://www.outleys.site/sitemap.xml instead of https://outleys.site/sitemap.xml I get an error: DoesNotExist at /sitemap.xml. Site matching query does not exist.
8.5.1. I figured out it depends from Admin Panel->Sites domain name if you set www.outleys.site instead outleys.site it will work other way around. Add both domains.
9. I have to make function to populate sitemap template manually because I can't get proper sitemap with django parler.
9.1. Do not name template as sitemap.xml and to view.py render add content_type="text/xml"