python
41 lines · 2 tabs
Priya Sharma
Jan 2026
2 tabs
from django.contrib.sitemaps import Sitemap
from .models import Post
class PostSitemap(Sitemap):
changefreq = 'weekly'
priority = 0.9
def items(self):
return Post.objects.filter(status='published')
def lastmod(self, obj):
return obj.updated_at
def location(self, obj):
return obj.get_absolute_url()
class StaticViewSitemap(Sitemap):
priority = 0.5
changefreq = 'monthly'
def items(self):
return ['home', 'about', 'contact']
def location(self, item):
from django.urls import reverse
return reverse(item)
from django.contrib.sitemaps.views import sitemap
from django.urls import path
from blog.sitemaps import PostSitemap, StaticViewSitemap
sitemaps = {
'posts': PostSitemap,
'static': StaticViewSitemap,
}
urlpatterns = [
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
]
2 files · python
Explain with highlit
Sitemaps help search engines discover and index pages. Django's sitemap framework generates XML sitemaps automatically. I create sitemap classes for each content type, defining items(), lastmod(), changefreq(), and priority(). For large sites, I use sitemap indexes. I configure in urls.py and submit to search engines. Dynamic sitemaps update automatically as content changes. This improves SEO without manual sitemap maintenance. I cache sitemaps for performance on high-traffic sites.
Related snips
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Semantic HTML Example</title>
Semantic HTML5 elements and accessibility best practices
html
html5
semantics
by Alex Chang
2 tabs
python
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
cost = models.DecimalField(max_digits=10, decimal_places=2)
margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
Django model signals vs overriding save
django
python
models
by Priya Sharma
2 tabs
python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
Django GraphQL with Graphene
django
python
graphql
by Priya Sharma
2 tabs
python
from django.db.models import Count, Avg, Sum, Q, F
from django.views.generic import TemplateView
from products.models import Product, Order, OrderItem
class DashboardView(TemplateView):
Django aggregation with annotate for statistics
django
python
database
by Priya Sharma
1 tab
Share this code
Here's the card — post it anywhere.