Django: Sitemap Sitemap, common view, and context renderer

Source: Internet
Author: User

Django has its own sitemap framework for generating XML files

Django Sitemap Demo:

Sitemap is very important, can be used to inform the Search engine page address, the importance of the page, help the site to get a better collection.

To turn on the sitemap feature

Django.contrib.sitemaps and Django.contrib.sites in the settings.py file are to be in Install_apps

Installed_apps = (    ' django.contrib.admin ',    ' Django.contrib.auth ',    ' django.contrib.contenttypes ',    ' django.contrib.sessions ',    ' django.contrib.messages ',    ' django.contrib.staticfiles ',    ' Django.contrib.sites ',    ' django.contrib.sitemaps ',    ' django.contrib.redirects ',         #####    #othther Apps    #####)

Django 1.7 and Previous versions:

Template_loaders to join the ' django.template.loaders.app_directories. Loader ', like this:

Template_loaders = (    ' django.template.loaders.filesystem.Loader ',    ' Django.template.loaders.app_ Directories. Loader ',)

Django 1.8 and later adds a new templates setting, where App_dirs is true, such as:

# Notice:code for Django 1.8, no work on Django 1.7 and belowtemplates = [    {        ' backend ': ' Django.template.backend S.django.djangotemplates ',        ' DIRS ': [            os.path.join (Base_dir, ' Templates '). replace (' \ \ ', '/'),        ],        ' App_dirs ': True,    },]

Then configure the following in the urls.py:

From django.conf.urls import urlfrom django.contrib.sitemaps import Genericsitemapfrom django.contrib.sitemaps.views Import sitemap from blog.models import Entry  sitemaps = {    ' blog ': Genericsitemap ({' Queryset ': Entry.objects.all (), ' Date_field ': ' Pub_date '}, priority=0.6),    # If you want to add another can imitate the above} urlpatterns = [    # Some generic view using Info_d ICT    # ...     # the Sitemap    url (r ' ^sitemap\.xml$ ', sitemap, {' Sitemaps ': sitemaps},        name= ' Django.contrib.sitemaps.views.sitemap '),

But this generated sitemap, if the site content too much is very slow, very resource-intensive, you can use the paging function:

From django.conf.urls import urlfrom django.contrib.sitemaps import Genericsitemapfrom django.contrib.sitemaps.views Import sitemap from blog.models import Entry from django.contrib.sitemaps import views as Sitemaps_viewsfrom Django.views. Decorators.cache Import cache_page  sitemaps = {    ' blog ': Genericsitemap ({' Queryset ': Entry.objects.all (), ' Date_field ': ' Pub_date '}, priority=0.6),    # If you want to add other can imitate the above} urlpatterns = [    url (R ' ^sitemap\.xml$ ',        cache _page (86400) (Sitemaps_views.index),        {' sitemaps ': sitemaps, ' sitemap_url_name ': ' Sitemaps '}),    url (r ' ^ sitemap-(? p<section>.+) \.xml$ ',        cache_page (86400) (Sitemaps_views.sitemap),        {' Sitemaps ': sitemaps}, Name= ' Sitemaps '),]

This allows you to see a sitemap similar to the following if the local test accesses the Http://localhost:8000/sitemap.xml

  

  

  

  

  

Django: Sitemap Sitemap, common view, and context renderer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.