我有一个基于模板的视图似乎无法正常工作(或者至少似乎无法在需要渲染它的页面上工作)。
以下是视图:
class LocationManager(View):
template_name = "dash/LocationManager.html"
def get(self, request, *args, **kwargs):
try:
user = User.objects.get(username=request.user.username)
locations = user.get_profile().owned_locations
return render(request, self.template_name, {'locations': locations})
except:
return render(request, self.template_name)
以下是与此视图有关的模型:
#in Location models
class Location(models.Model):
region = models.ForeignKey(Region)
manager = models.ForeignKey(User)
name = models.CharField(max_length=255)
street_address = models.TextField(blank=True)
city = models.CharField(max_length=255, blank=True)
zip_code = models.CharField(max_length=20, blank=True)
#in UserProfile models
class UserProfile(models.Model):
user = models.OneToOneField(User)
api_key = models.TextField()
pp_api_key = models.TextField(blank=True)
owned_beacons = models.ManyToManyField(
Beacon,
blank=True,
null=True,
related_name='owned_beacons'
)
owned_locations = models.ManyToManyField(
Location,
blank=True,
null=True,
related_name='owned_locations'
)
def __unicode__(self):
return u'%s' % self.user.username
最后是模板:
{% for location in locations.all %}<tr>
<td>{{location.name}}</td>
<td>{{location.street_address}}</td>
<td>{{location.zip_code}}</td>
<td>{{location.region}}
</tr>
{% endfor %}
然而,模板不会呈现与表单逻辑相关的任何内容(模板的其余部分加载正常)。没有错误,这就是为什么这让我很难理解为什么模板逻辑/视图不能正常工作。任何想法都会非常有用。
答案 0 :(得分:1)
尝试{%for location in locations%}