Many people know that Django provides the sitemaps function, that is, displaying website-related projects in sitemap. xml:
Generally, add the following method to URLs. py:
From Django. contrib. sitemaps import flatpagesitemap, genericsitemap
From ddtcms. Blog. sitemaps import blogsitemap
From ddtcms. Blog. Models import entry
Info_dict = {
'Queryset': entry. Objects. All (),
'Date _ field': 'pub _ date ',
}
Sitemaps = {
'Flatpags': flatpagesitemap,
'Blog ': blogsitemap,
}
Add the following to urlpatterns:
(R '^ sitemap. xml $', 'django. contrib. sitemaps. Views. sitemap', {'sitemaps ': sitemaps }),
# (R '^ sitemap. xml $', 'django. contrib. sitemaps. Views. Index', {'sitemaps ': sitemaps }),
(R' ^ sitemap -(? P <section>. +). xml $ ', 'django. contrib. sitemaps. Views. sitemap', {'sitemaps': sitemaps }),
Then access http: // 127.0.0.1: 8000/sitemap. XML in the browser.
I did it according to the above practice, but the following error may occur:
Attributeerror at/sitemap. xml
'Site' object has no attribute 'flatpage_set'
| Request Method: |
Get |
| Request URL: |
Http: // localhost/sitemap. xml |
| Exception type: |
Attributeerror |
| Exception value: |
'Site' object has no attribute 'flatpage_set' |
| Exception location: |
C:/python25/lib/Site-packages/Django/contrib/sitemaps/_ init _. py in items, line 81 |
This is about 'site 'object has no attribute 'flatpage _ set'. After reading the Django source code, I don't know what is going on.
Later, I checked some Django apps on Google and finally solved the above error.
The method is:
Add
Installed_apps = (
'Django. contrib. auth ',
'Django. contrib. Sites ',
'Django. contrib. contenttypes ',
'Django. contrib. session ',
......
'Django. contrib. flatpage', <add this
Middleware_classes = (
....
'Django. contrib. flatpages. Middleware. flatpagefallbackmiddleware ', <add this
Re-AccessHttp: // MAID: 8000/sitemap. xmlTry? The entry of sitemap defined in the blog module is displayed.
It's easy, isn't it?