2011-02-14 51 views
7

我跟隨在http://docs.djangoproject.com/en/1.2/ref/contrib/sitemaps/Django的網站地圖:「模塊」對象沒有屬性「值」

描述我從django.contrib中導入站點地圖此行添加

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

到URL配置

使文件sitemap.py與:

from django.contrib.sitemaps import Sitemap 
from blog.models import Post 

class BlogSitemap(Sitemap): 
    changefreq = 'monthly' 
    priority = 0.5 

    def items(self): 
     return Post.objects.all() 

    def lastmod(self, obj): 
     return obj.date 

在此地址http://127.0.0.1:8000/sitemap.xml我收到一個錯誤:

Traceback: 
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    100.      response = callback(request, *callback_args, **callback_kwargs) 
File "/usr/lib/python2.7/site-packages/django/contrib/sitemaps/views.py" in sitemap 
    33.   maps = sitemaps.values() 

Exception Type: AttributeError at /sitemap.xml 
Exception Value: 'module' object has no attribute 'values' 

任何人都可以幫助我嗎?

+0

/views.py評論地圖= sitemaps.values()和打印目錄(網站地圖)...告訴我們它顯示了什麼。 – panchicore 2011-02-14 19:26:04

+0

['FlatPageSitemap','GenericSitemap','錯誤配置','PING_URL','Site','Sitemap','SitemapNotFound','__builtins__','__doc__','__file__','__name__','__package__', '__path__','get_current_site','models','paginator','ping_google','urllib','urlresolvers','views'] – 2011-02-14 19:45:38

回答

7

你錯過了一個步驟 - 看看example in the documentation

而是在你的urls.py導入網站地圖模塊,導入BlogSitemap類,然後創建一個網站地圖詞典:

sitemaps = {'blog': BlogSitemap} 
6

我遇到過這個問題爲好。在我看到的文檔和代碼示例之間,我仍然不明白爲什麼我看到這個錯誤。

對我而言的誤讀文檔是從https://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/

It may also map to an instance of a Sitemap class (e.g., BlogSitemap(some_var)).

我又看了看Django的來源有點接近。觀點如下(django.contrib.sitemaps.views.sitemap):

def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'): 
    maps, urls = [], [] 
    if section is not None: 
     if section not in sitemaps: 
      raise Http404("No sitemap available for section: %r" % section) 
     maps.append(sitemaps[section]) 
    else: 
     maps = sitemaps.values() # This is where I was seeing the error. 
    page = request.GET.get("p", 1) 
    current_site = get_current_site(request) 
    for site in maps: 
     try: 
      if callable(site): 
       urls.extend(site().get_urls(page=page, site=current_site)) 
      else: 
       urls.extend(site.get_urls(page=page, site=current_site)) 
     except EmptyPage: 
      raise Http404("Page %s empty" % page) 
     except PageNotAnInteger: 
      raise Http404("No page '%s'" % page) 
    xml = smart_str(loader.render_to_string(template_name, {'urlset': urls})) 
    return HttpResponse(xml, mimetype='application/xml') 

它然後我突然明白了,參數網站地圖竟是字典關鍵站點地圖對象,而不是一個網站地圖對象本身。這可能對我來說很明顯,但我需要一點點才能克服我的心理障礙。

,我用類似以下的全編碼樣本:

sitemap.py文件:

from django.contrib.sitemaps import Sitemap 
from articles.models import Article 

class BlogSitemap(Sitemap): 
    changefreq = "never" 
    priority = 0.5 

    def items(self): 
     return Article.objects.filter(is_active=True) 

    def lastmod(self, obj): 
     return obj.publish_date 

urls.py文件:

from sitemap import BlogSitemap 

# a dictionary of sitemaps 
sitemaps = { 
    'blog': BlogSitemap, 
} 

urlpatterns += patterns ('', 
    #...<snip out other url patterns>... 
    (r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), 
) 
Sitemap中