how-to tips   0   805
Creating sitemap of Dynamic URLs in your Django Application

A site map is a list of a website's content designed to help both users and search engines navigate the site.

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.


Why sitemaps is required:

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.


Example:
Sitemap for this blog can be found at http://thepythondjango.com/sitemap_index.xml

creating sitemap of dynamic urls in your django application


Steps to add Sitemaps to your Django Application:

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.

Lets assume your website sell some product where product details are stored in database. Once a new product is added to database, you want that product page to be searchable by search engines. We need to add all such product pages/urls to sitemaps.


Static Sitemap:
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 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 absolute URL.

Refer you app's urls.py file for url names.


Dynamic Sitemap:
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 database and generating URLs like http:example.com/product/12.



Adding sitemaps in URLconf:
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$', sitemap, {'sitemaps': sitemaps})
]
 

Now reload your server and go to localhost:8000/sitemap.xml and you will be able to see your sitemap there.  


Reference : 
https://docs.djangoproject.com/en/2.0/ref/contrib/sitemaps/  

Host your Django App for Free.

how-to tips   0   805

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
How to upgrade to paid account on PythonAnyWhere
Which is the best server for hosting Django Apps. Best hosting provider for Django Apps. Cheapest Django Hosting. PythonAnyWhere Reviews. Django Hosting....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scrapping large amount of tweets within minutes using celery, RabbitMQ and docker cluster. Scraping huge data quickly using docker cluster with TOR....

0 thoughts on 'Creating Sitemap Of Dynamic Urls In Your Django Application'
Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:






© pythoncircle.com 2018-2019