Django站点地图:http://example.com

28

我尝试创建一个包含我的Django项目的站点地图sitemap.xml。

在我的项目中,我没有模型(我的数据库为空),我只有静态URL(例如“home”或“About”)。

我成功生成了sitemap.xml,但是其中显示的是“http://example.com”,而不是我的域名。

以下是我的sitemap.xml:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>
http://example.com/accueil
</loc>
<lastmod>
2013-03-14
</lastmod>
<changefreq>
weekly
</changefreq>
</url>
<url>
<loc>
http://example.com/cv
</loc>
<lastmod>
2013-03-14
</lastmod>
<changefreq>
weekly
</changefreq>
</url>
<url>
<loc>
http://example.com/portfolio
</loc>
<lastmod>
2013-03-14
</lastmod>
<changefreq>
weekly
</changefreq>
</url>
<url>
<loc>
http://example.com/a_propos
</loc>
<lastmod>
2013-03-14
</lastmod>
<changefreq>
weekly
</changefreq>
</url>
</urlset>

我的urls.py文件如下:

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static

from sitemaps import BasicSitemap
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

sitemaps= {
    'pages' : BasicSitemap(['accueil','cv','portfolio','apropos'])
}

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'portfolio.views.home', name='home'),
    # url(r'^portfolio/', include('portfolio.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('portail_portfolio.urls')),
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})

)

urlpatterns += staticfiles_urlpatterns() + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的sitemaps.py文件:

from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse
#from portail_portfolio.models import Entry

from datetime import datetime

class BasicSitemap(Sitemap):

    def __init__(self, names):
        self.names = names

    def items(self):
        return self.names
    
    def changefreq(self, obj):
        return 'weekly'

    def lastmod(self, obj):
        return datetime.now()

    def location(self,obj):
        return reverse(obj)
1个回答

54

django.contrib.sitemaps 依赖于 django.contrib.sites

进入你的管理后台 /admin/sites/site/1/ 并更改你在那里看到的域名。


иҝҷйҮҢжңүдёҖдәӣе…ідәҺSitesжЎҶжһ¶зҡ„зӣёе…ійҳ…иҜ»жқҗж–ҷпјҡhttps://docs.djangoproject.com/en/dev/ref/contrib/sites/ - Timmy O'Mahony
太棒了,谢谢。这正是我在设置一个非常基本的站点地图并在本地主机上尝试时遇到“TemplateDoesNotExist:sitemap.xml”错误的原因:我的settings.py中的SITE_ID硬编码为“domain.com”,所以它在本地无法工作。 - Mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接