Django:模板逻辑没有呈现查询数据,也没有给出错误
我有一个基于模板的视图似乎不起作用(或者至少在需要呈现它的页面上似乎不起作用) 以下是视图:Django:模板逻辑没有呈现查询数据,也没有给出错误,django,django-models,django-forms,django-templates,django-views,Django,Django Models,Django Forms,Django Templates,Django Views,我有一个基于模板的视图似乎不起作用(或者至少在需要呈现它的页面上似乎不起作用) 以下是视图: class LocationManager(View): template_name = "dash/LocationManager.html" def get(self, request, *args, **kwargs): try: user = User.objects.get(username=request.user.username) locat
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 %}
{%用于locations.all%}
{{location.name}
{{location.street{u address}}
{{location.zip_code}
{{location.region}
{%endfor%}
但是,模板不会呈现与表单逻辑相关的任何内容(模板的其余部分可以正常加载)。也没有出现错误,这就是为什么我很难理解为什么模板逻辑/视图不能正常工作的原因。任何想法都会非常有帮助。请尝试{%查找位置%}谢谢您的建议。我最终摆脱了.all,它似乎工作得更好,但我的主要问题是Try/Except。解决这两个问题最终使它起了作用。谢谢你!