A site map can be a hierarchical list of pages, an organization chart, or an XML document that provides instructions to search engine crawl bots.
XML Sitemaps are important for SEO because they make it easier for Google to find your site’s pages—this is important because Google ranks web PAGES, not just websites. There is no downside of having an XML Sitemap and having one can improve your SEO, so we highly recommend them.
The sitemap for this blog can be found at http://thepythondjango.com/sitemap_index.xml .
Create a file sitemap.py
in your app.
Create two different classes in sitemap.py
file, one for static pages and another for Dynamic URLs.
Let’s assume your website sell some product where product details are stored in the database. Once a new product is added to the database, you want that product page to be searchable by search engines. We need to add all such product pages/URLs to sitemaps.
Define a class StaticSitemap
in your sitemap.py
file. Define the mandatory function items in it which will return the list of objects.
These objects will be passed to the location method which will create URL from these objects.
from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
class StaticSitemap(Sitemap):
def items(self):
return [
'myapp:terms_and_conditions',
'myapp:contact_us',
'myapp:about_us'
]
def location(self, item):
return reverse(item)
Here in items function, we are returning appname:url_name
which will be used by location method to convert into an absolute URL.
Refer you app’s urls.py
file for URL names.
Similarly, we will create Dynamic sitemap by fetching values from DB.
from mystore.models import ProductDetailsModel
class ProductSitemap(Sitemap):
def items(self):
return ProductDetailsModel.objects.all()
def location(self, item):
return reverse('myapp:product', args=[item.product_id])
Here we are getting all products from the database and generating URLs like http:example.com/product/12
.
Now add these sitemap class in URLconf. Edit the project’s urls.py file and add below code in it.
from mystore.sitemap import StaticSitemap, ProductSitemap
from django.contrib.sitemaps.views import sitemap
sitemaps = {
'pages': StaticSitemap,
'products': ProductSitemap,
}
urlpatterns += [
url(r'^sitemap.xml
Now reload your server and go to ```localhost:8000/sitemap.xml``` and you will be able to see your sitemap there.
*Originally published **** ****at *[***pythoncircle.com***](https://www.pythoncircle.com/post/584/creating-sitemap-of-dynamic-urls-in-your-django-application/ "***pythoncircle.com***")
===================================================================
Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on [**Facebook**](https://www.facebook.com/Pythontutorial4u "**Facebook**") | [**Twitter**](https://twitter.com/codek_tv "**Twitter**")
## Learn More
☞ [Complete Python Bootcamp: Go from zero to hero in Python 3](http://learnstartup.net/p/S15_M7e-l "Complete Python Bootcamp: Go from zero to hero in Python 3")
☞ [Python and Django Full Stack Web Developer Bootcamp](http://academy.learnstartup.net/p/r1-quFMgce "Python and Django Full Stack Web Developer Bootcamp")
☞ [Python for Time Series Data Analysis](http://learnstartup.net/p/OqjO6VL2y "Python for Time Series Data Analysis")
☞ [Python Programming For Beginners From Scratch](http://learnstartup.net/p/d3qnutpk4 "Python Programming For Beginners From Scratch")
☞ [Python Network Programming | Network Apps & Hacking Tools](http://learnstartup.net/p/QRQDxXvpZ "Python Network Programming | Network Apps & Hacking Tools")
☞ [Intro To SQLite Databases for Python Programming](http://learnstartup.net/p/7u6CWaas2 "Intro To SQLite Databases for Python Programming")
☞ [Ethical Hacking With Python, JavaScript and Kali Linux](http://learnstartup.net/p/aRaBXiAoZ "Ethical Hacking With Python, JavaScript and Kali Linux")
☞ [Beginner’s guide on Python: Learn python from scratch! (New)](http://learnstartup.net/p/szT1QzYPf "Beginner’s guide on Python: Learn python from scratch! (New)")
☞ [Python for Beginners: Complete Python Programming](http://learnstartup.net/p/LMc19YJl6 "Python for Beginners: Complete Python Programming")
☞ [Django 2.1 & Python | The Ultimate Web Development Bootcamp](http://academy.learnstartup.net/p/rJmJxyM9M "Django 2.1 & Python | The Ultimate Web Development Bootcamp")
☞ [Python eCommerce | Build a Django eCommerce Web Application](http://academy.learnstartup.net/p/H19Qb1nkf "Python eCommerce | Build a Django eCommerce Web Application")
☞ [Python Django Dev To Deployment](http://academy.learnstartup.net/p/9yr7V_-O8 "Python Django Dev To Deployment")
, sitemap, {'sitemaps': sitemaps})
]
Now reload your server and go to localhost:8000/sitemap.xml
and you will be able to see your sitemap there.
#python #django #web-development