【问题标题】:Creating sitemap on Django error: object has no attribute '__getitem__'在 Django 错误上创建站点地图:对象没有属性“__getitem__”
【发布时间】:2015-01-29 17:20:19
【问题描述】:

我是 Django 的初学者。我尝试生成 sitemap.xml,但出现以下错误:

   'Dish' object has no attribute '__getitem__'

我一直在寻找here,但无法真正实施任何可行的解决方案 这是我的代码:

站点地图.py

from django.contrib.sitemaps import Sitemap
from wm.models import Dish


class todositemap(Sitemap):
    changefreq = "weekly"
    priority = 0.5

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

    def location(self, obj):
        return obj['slug']

模型.py

class Dish(models.Model):
    name =  models.CharField(max_length=64)
    article = models.CharField(max_length=10000, blank=True)
    magasines = models.ManyToManyField(Magasine, blank=True)
    category = models.ForeignKey(DishCategory, blank=True, null = True)
    geo_category = models.ManyToManyField(GeoCategory, blank=True)
    catchphrase = models.CharField(max_length=1000, blank=True, null = True)
    local_name = models.CharField(max_length=64, blank=True)
    phonetics = models.CharField(max_length=64, blank=True)
    picture = models.ImageField(upload_to='dishes', default='no-image-available.jpg', blank=True, null = True)
    picture_small = models.ImageField(upload_to='dishes', default='no-image-available.jpg', blank=True, null = True)
    date =  models.DateTimeField('date published', blank=True, null = True)
    likes = models.IntegerField(default=0, blank=True)
    topN = models.IntegerField(default=0, blank=True)

    def __unicode__(self):
        return unicode(self.name) 

追溯:

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/sitemap.xml

Django Version: 1.7
Python Version: 2.7.7
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.sites',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'wm',
  'storages',
  'django.contrib.sitemaps')
 Installed Middleware:
 ('django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware')


 Traceback:
 File "C:\Users\laurent\Desktop\venv\lib\site-packages\django\core\handlers\base.py" in get_response
   111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "C:\Users\laurent\Desktop\venv\lib\site-packages\django\contrib\sitemaps\views.py" in inner
   16.         response = func(request, *args, **kwargs)
 File "C:\Users\laurent\Desktop\venv\lib\site-packages\django\contrib\sitemaps\views.py" in sitemap
   67.                                       protocol=req_protocol))
 File "C:\Users\laurent\Desktop\venv\lib\site-packages\django\contrib\sitemaps\__init__.py" in get_urls
   96.             loc = "%s://%s%s" % (protocol, domain, self.__get('location', item))
 File "C:\Users\laurent\Desktop\venv\lib\site-packages\django\contrib\sitemaps\__init__.py" in __get
   60.             return attr(obj)
 File "C:\Users\laurent\Desktop\venv\wm_v2\wmsite\sitemap.py" in location
   16.                 return obj['slug']

 Exception Type: TypeError at /sitemap.xml
 Exception Value: 'Dish' object has no attribute '__getitem__'

非常感谢

【问题讨论】:

  • 什么版本的 Python?
  • 2.7.7 根据回溯
  • @lauWM 您的模型Dish 不包含slug 属性。
  • 添加了 def location(self, obj): return obj['slug'] 但没有解决问题

标签: python django


【解决方案1】:

这里有两个错误。

obj 将是一个 Dish 实例,由 items 方法返回。模型实例与任何其他类实例一样,它们使用点表示法而不是方括号表示法。

其次,您的 Dish 模型没有 slug 属性。所以你不能从方法中返回它。

【讨论】:

  • obj.slug,就像您在模板或视图中的其他任何地方所做的一样。但是你没有 slug 属性,所以这无论如何都行不通。
  • 我在菜类中添加了def location(self, obj): return obj['slug'],对吗?
  • 不,当然不是。为什么会是正确的?你仍然犯同样的两个错误,而且你没有理由打电话给dish.location
猜你喜欢
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 2015-04-19
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 2018-05-09
相关资源
最近更新 更多