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',
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
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
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>
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 .