程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How Django uses sitemap to implement site map

編輯:Python

A site map is a collection of all the links in a site , Search engines can easily capture you based on the site map sitemap The web address recorded inside , So submit the site map to the search engine , Let them enter your content , It is a very important means to improve your website traffic , Especially for new websites , The website map is SEO Necessary means , Here is a brief introduction Django How to quickly generate a site map sitemap

1. install sitemap

sitemap It's a app, So use it , You need to install this first app, In the project setting.py Of documents INSTALLED_APPS in , Add the following :

'django.contrib.sitemaps',

2. Realization sitemap Generate the file

In the project app Under the new sitemaps.py file , For example, you should put the links of blog articles into sitemap Inside , stay blog app Under the new sitemap.py file , Definition ArticleSitemap class , Inherit Django Class Sitemap, as follows :

class ArticleSitemap(Sitemap):
changefreq = 'weekly'
priority = 1.0
def items(self):
return Article.objects.all()
def lastmod(self, obj):
if obj.update_date:
return obj.update_date
return obj.add_time

Yes Sitemap class , Don't forget import it

from django.contrib.sitemaps import Sitemap

a. changefreq and priority Corresponding sitemap.xml Inside changefreq and priority.

b. item The method is to return all your articles object,locate() Would be right item Back to object To call get_absolute_url Method , This value will be placed in xml Inside loc The location of .

c. therefore , If you don't rewrite locate Method , You need to be in the corresponding model To implement inside get_absolute_url Method , For example, here is Article Of model It needs to be realized get_absolute_url Method , Note here that the parameters must be the same as url The configuration matches . Because it's used reverse, So you need to import .

from django.urls import reverse
def get_absolute_url(self):
return reverse('blog:article_detail', kwargs={'article_id': self.id})

d. lastmod It also corresponds to xml Inside lastmod

e. This completes a model Of sitemap Realization , The same method can be used to implement other needs sitemap Inside model

3. send sitemap take effect

In the project url.py Inside , Join in sitemap.py Classes implemented inside , as follows : If there are other implemented classes , Add to sitemaps In the dictionary .

from blog.sitemaps import ArticleSitemap
sitemaps = {
'articles': ArticleSitemap
}

stay urlpatterns Join in url, as follows :

url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),

It's used here sitemap, So we need to import This module

from django.contrib.sitemaps.views import sitemap

4. front end html Page setup

At the bottom of the site , Add according to page layout sitemap Hyperlinks for , as follows

<a href="/sitemap.xml" title="Sitemap" target="_blank"> Website map </a>

5. See the effect

If these are configured , Then you can enter... In the browser Web site address /sitemap.xml see , You can also click the website map at the bottom of the website , Will automatically jump to the website address /sitemap.xml, For example, my website map is in http://www.0a0z.cn/sitemap.xml, You can see the format of the website map .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved