Python Django:无法将模型中的用户配置文件数据插入模板

Python Django:无法将模型中的用户配置文件数据插入模板,python,django,django-models,django-templates,django-views,Python,Django,Django Models,Django Templates,Django Views,我正在使用Django的默认用户身份验证,并且创建了一个单独的模型来稍微扩展用户配置文件。当我尝试访问用户配置文件信息时,它不会显示在页面上。在我的视图中,我将纵断面对象传递到视图的上下文,但它仍然不起作用 当我在shell中尝试它时,我得到了AttributeError:'QuerySet'对象没有属性'country' 执行以下操作时出错: profile = Profile.get.objects.all() country = profile.coutry country 下面是my

我正在使用Django的默认用户身份验证,并且创建了一个单独的模型来稍微扩展用户配置文件。当我尝试访问用户配置文件信息时,它不会显示在页面上。在我的视图中,我将纵断面对象传递到视图的上下文,但它仍然不起作用

当我在shell中尝试它时,我得到了AttributeError:'QuerySet'对象没有属性'country' 执行以下操作时出错:

profile = Profile.get.objects.all()
country = profile.coutry
country
下面是my models.py:

from pytz import common_timezones
from django.db import models
from django.contrib.auth.models import User
from django_countries.fields import CountryField
from django.db.models.signals import post_save
from django.dispatch import receiver


TIMEZONES = tuple(zip(common_timezones, common_timezones))


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    country = CountryField()
    timeZone = models.CharField(max_length=50, choices=TIMEZONES, default='US/Eastern')

    def __str__(self):
        return "{0} - {1} ({2})".format(self.user.username, self.country, self.timeZone)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
这是我的观点

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from user.models import Profile

@login_required()
def home(request):
    profile = Profile.objects.all()
    return render(request, "user/home.html", {'profile': profile}) 
最后是home.html文件:

{% extends "base.html" %}

{% block title %}
Account Home for {{ user.username }}
{% endblock title %}

{% block content_auth %}
    <h1 class="page-header">Welcome, {{ user.username }}. </h1>

<p>Below are you preferences:</p>

<ul>
    <li>{{ profile.country }}</li>
    <li>{{ profile.timeZone }}</li>
</ul>
{% endblock content_auth %}
{%extends“base.html”%}
{%block title%}
{{user.username}的主帐户
{%endblock title%}
{%block content_auth%}
欢迎,{{user.username}}。
以下是您的首选项:

  • {{profile.country}
  • {{profile.timeZone}
{%endblock内容\u auth%}
现在配置文件中有很多记录,因为您有
get.objects.all()
。所以用那种方式使用它

profiles = Profile.get.objects.all()

# for first profile's country
country1 = profiles.0.country

#for second profile entry
country2 = profiles.1.country
或者在html中

对于特定用户,获取该用户的
id
,然后获取其个人资料

现在是html

{{profile.country}}
{{profile.timezone}}

如果我只想显示特定登录用户的配置文件数据,该怎么办?我是否必须更改我的模型/视图?请参阅我已更新我的答案。看一看,告诉我它是否对你有效。如果这个解决方案对你有效。你可以投票选择我的答案。谢谢你的合作。我一有空就做。
id = request.user.pk
profile = get_object_or_404(Profile, user__id=id)
{{profile.country}}
{{profile.timezone}}