无法从html访问对象

时间:2019-06-16 13:04:52

标签: python django

我正在制作一个django项目,想从html中访问用户的特征,但无法这样做,有人可以在我的代码中指出错误吗?

html

  <img height="125px" width="125px" class="rounded-circle article-img" src="{{ user1.image.url }}">

      <h5 class='pr-2'>{{ user1.user }} </h5>

views.py

def user_blog_list(request, username):
    user1=Profile.objects.filter(user__username=username)
    context={
    'user1':user1
    }
    return render(request,'blog/user_posts.html',context)

models.py

class Profile(models.Model):
    user=models.OneToOneField(User, on_delete=models.CASCADE)
    image=models.ImageField(default='default.jpg',upload_to='profile_pics',blank=True)
    description=models.TextField(max_length=200, blank=True)

    def __str__(self):
        return f'{self.user.username} Profile'

urls.py

path('user/<str:username>',views.user_blog_list,name='user-posts'),

1 个答案:

答案 0 :(得分:0)

filter始终返回QuerySet。您需要get

user1=Profile.objects.get(user__username=username)