Python 在django中的模板中显示来自模型的用户protrait
我想在模板中显示用户的用户保护 models.pyPython 在django中的模板中显示来自模型的用户protrait,python,django,django-models,django-templates,django-views,Python,Django,Django Models,Django Templates,Django Views,我想在模板中显示用户的用户保护 models.py class Profile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) date_of_birth = models.DateField(blank=True, null=True) photo = models.ImageField(upload_to='user/%Y/%m
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_of_birth = models.DateField(blank=True, null=True)
photo = models.ImageField(upload_to='user/%Y/%m/%d/', blank=True)
views.py
@login_required
def dashboard(request):
# Display all actions by default
actions = Action.objects.exclude(user=request.user)
following_ids = request.user.following.values_list('id',
flat=True)
if following_ids:
# If user is following others, retrieve only their actions
actions = actions.filter(user_id__in=following_ids)
actions = actions.select_related('user', 'user__profile')\
.prefetch_related('target')[:10]
return render(request,
'account/dashboard.html',
{'section': 'dashboard',
'actions': actions})
tempate:
<img src="{{ Profile.photo.url }} " height="40" width="40"/>
您可以使用:
<img src="{{ request.user.profile.photo.url }} " height="40" width="40"/>
由于django的
用户
模型与您的配置文件
模型之间存在一对一关系。您想显示登录用户的图像吗?