有兩種方案
第一種方案、
將相應(yīng)的sitemap文件放入網(wǎng)站的templates文件夾中同時在網(wǎng)站的根urls文件中添加如下代碼
from django.views.generic import TemplateView
urlpatterns = [
re_path(r'^sitemap\.xml', TemplateView.as_view(template_name='sitemap.xml',content_type='text/xml')),
re_path(r'^sitemap\.html$', TemplateView.as_view(template_name='sitemap.html',content_type='text/html')),
re_path(r'^sitemap\.txt$', TemplateView.as_view(template_name='sitemap.txt',content_type='text/text')),
]
urls.py中加入新的urlpattern,用TemplateView去展示
第二種方案、
直接交給nginx來處理,在nginx的conf文件中加入要處理的static URL和路徑
location /sitemap.xml {
alias /path/to/static/sitemap.xml;
}
我有的第一種方法測試成功,可以成功被百度等識別,第二種的我沒測試
參考:http://stackoverflow.com/questions/18424260/django-serving-robots-txt-efficiently