Информация о модели Django не отображается
Я пытаюсь создать образовательный сайт с помощью Django, и когда я пытаюсь отобразить {{ profile.institution }}
или {{ profile.grade }}
, они не отображаются. Хотя {{ user.username }}
отображается. Я не знаю, почему они не отображаются. Может ли кто-нибудь помочь мне решить эту проблему?
Мой models.py:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
institution = models.CharField(max_length = 100)
grade = models.CharField(max_length=100, choices= YEAR_IN_SCHOOL_CHOICES)
bio = models.TextField(max_length=300)
def __str__(self):
return f'{self.user.username} Profile'
Мой views.py:
def User_Profile(request, user):
model = Profile
user = request.user
profile = Profile.objects.filter(user=user)
context = {'profile': profile}
return render(request, 'class/profile.html', context)
Мой html:
<div class="container mt-5">
<div class="row d-flex justify-content-center">
<div class="col-md-7">
<div class="card p-3 py-4">
<div class="text-center">
<i class='fas fa-user-alt' style='font-size:36px'></i>
<!-- <img src="" width="100" class="rounded-circle"> -->
</div>
<div class="text-center mt-3">
<span class="bg-secondary p-1 px-4 rounded text-white">Pro</span>
<h5 class="mt-2 mb-0">{{ user.username }}</h5>
<span>{{ profile.institution }}</span>
<span>{{ profile.grade }} Grade</span>
<div class="px-4 mt-1">
<p class="fonts">{{ profile.bio }}</p>
</div>
<div class="buttons">
<button class="btn btn-outline-primary px-4">Message</button>
<button class="btn btn-primary px-4 ms-3">Contact</button>
</div>
</div>
</div>
</div>
</div>
</div>
profile = Profile.objects.filter(user=user)
присвоит кверисет профилю.
.
Для доступа к profile.institution вам понадобится, чтобы profile был объектом, а не набором queryset, поэтому вам нужно будет использовать get
вместо filter
:
.
profile = Profile.objects.get(user=user)