我在Django是个初学者。我尝试生成一个网站地图.xml,但我得到以下错误:
'Dish' object has no attribute '__getitem__'
我一直在寻找here,但无法真正实现任何可行的解决方案 这是我的代码:
在网站地图.py
^{pr2}$在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__'
非常感谢
这里有两个错误。在
obj
将是一个Dish实例,由items
方法返回。模型实例和其他类实例一样,它们使用点表示法而不是方括号表示法。在其次,您的Dish模型没有
slug
属性。所以不能从方法返回它。在相关问题 更多 >
编程相关推荐