Python 未显示相关模型字段

Python 未显示相关模型字段,python,html,django,Python,Html,Django,我很难理解代码中的错误。请告诉我为什么locations=Location.objects.filter(user=add\u profile.user)中的字段没有显示在我的html页面中 型号.py class Location(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) my_location = models.CharField(max_length=120, choice

我很难理解代码中的错误。请告诉我为什么
locations=Location.objects.filter(user=add\u profile.user)
中的字段没有显示在我的html页面中

型号.py

class Location(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    my_location = models.CharField(max_length=120, choices=LOCATION_CHOICES)
    update_date = models.DateField(auto_now=True, null=True)
    date = models.DateField()

def __str__(self):
    return self.my_location


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    user_base = models.CharField(max_length=120, choices=LOCATION_CHOICES)
    user_position = models.CharField(max_length=120)
    user_phone = models.CharField(max_length=50)
    first_name = models.CharField(max_length=120, null=True)
    last_name = models.CharField(max_length=120, null=True)
    slug = models.SlugField()

def save(self, *args, **kwargs):
    self.slug = slugify(self.user)
    super(UserProfile, self).save(*args, **kwargs)

def __unicode__(self):
    return self.user.username
@login_required
def details(request, user_slug):
    add_profile = UserProfile.objects.get(slug=user_slug)
    locations = Location.objects.filter(user=add_profile.user)
    print(locations)
    context = {'add_profile': add_profile, locations: "locations"}
    return render(request, 'details.html', context)
视图.py

class Location(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    my_location = models.CharField(max_length=120, choices=LOCATION_CHOICES)
    update_date = models.DateField(auto_now=True, null=True)
    date = models.DateField()

def __str__(self):
    return self.my_location


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    user_base = models.CharField(max_length=120, choices=LOCATION_CHOICES)
    user_position = models.CharField(max_length=120)
    user_phone = models.CharField(max_length=50)
    first_name = models.CharField(max_length=120, null=True)
    last_name = models.CharField(max_length=120, null=True)
    slug = models.SlugField()

def save(self, *args, **kwargs):
    self.slug = slugify(self.user)
    super(UserProfile, self).save(*args, **kwargs)

def __unicode__(self):
    return self.user.username
@login_required
def details(request, user_slug):
    add_profile = UserProfile.objects.get(slug=user_slug)
    locations = Location.objects.filter(user=add_profile.user)
    print(locations)
    context = {'add_profile': add_profile, locations: "locations"}
    return render(request, 'details.html', context)
不过,
print(locations)
正在mycmd内打印请求的数据

html代码

{%l在位置%}
  • {{l.my_location}
{%endfor%}
而不是

context={'add_profile':add_profile,locations:“locations”}

应该是

context={'add_profile':add_profile,'locations':locations}


你没有使用
locations
作为上下文的值,而是将它作为键和值,只作为字符串“locations”。

OMG,我现在觉得自己太蠢了。我看了那段代码一个小时(.