我在 views.py 中收到以下错误,但我无法找出原因。请帮忙。
Request Method: GET
Request URL: http://localhost:8000/tribalrights/
Django Version: 1.3.1
Exception Type: AttributeError
Exception Value: 'NoneType' object has no attribute 'all'
Exception Location: /home/gunjan/tribalrights/tr/views.py in home, line 70
Python Executable: /usr/bin/python
Python Version: 2.7.3
下面是在 views.py 中调用的函数。第 70 行开始于声明的下一行。
def home(request):
atrocities = Atrocity.objects.all()
cities = City.objects.all()
for atrocity in atrocities:
#this is line 69.below is line 70.
cities = atrocity.location.all()
return render_to_response('tr/home.html', {
'cities' : cities,
})
下面是 models.py 中 City 和 Atrocity 属性的定义
class Atrocity(models.Model):
name = models.CharField(max_length=255)
dateTimeOccurred = models.DateTimeField ( null=True, blank=True )
persons = models.ManyToManyField ( Person, null=True, blank=True )
atrocityType = models.ForeignKey(AtrocityType)
description = models.CharField(max_length=255)
location = models.ForeignKey(City, null=True, blank=True)
def __unicode__(self):
return self.name
class City(models.Model):
name = models.CharField(max_length=255)
district = models.ForeignKey(District)
latitude = models.DecimalField(max_digits=13, decimal_places=10, null=True, blank=True)
longitude = models.DecimalField(max_digits=13, decimal_places=10, null=True, blank=True)
def __unicode__(self):
return self.name