【发布时间】: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'] 但没有解决问题