Django,错误:UserProfile匹配查询不存在

Django,错误:UserProfile匹配查询不存在,django,Django,我在学django。现在,我需要创建一个Userprofile 我已经创建了一个 class UserProfile(models.Model): user = models.OneToOneField(User) active = models.BooleanField() address = models.CharField('Shipping address', max_length=150, blank=True, null=True) telephone = models.Ch

我在学django。现在,我需要创建一个Userprofile

我已经创建了一个

class UserProfile(models.Model):  
user = models.OneToOneField(User)  
active = models.BooleanField()
address = models.CharField('Shipping address', max_length=150, blank=True, null=True)
telephone = models.CharField('Telephone number for shipping', max_length=20, blank=True, null=True)
steps = models.DecimalField('Steps since creation', max_digits=100, decimal_places=2, null=True)
active = models.BooleanField()

def __str__(self):  
    return "%s's profile" % self.user  
在一个名为accounting的应用程序中。我已经创造了

def create_user_profile(sender, **kwargs):
#When creating a new user, make a profile for him or her.
u = kwargs["instance"]
if not UserProfile.objects.filter(user=u):
    UserProfile(user=u).save()
post\u save.connect(创建用户配置文件,发件人=用户)

因此,每次创建用户时,都会自动创建一个配置文件。我已经创建并验证了该用户是在userprofile表中创建的。我也去了贝壳。我寻找ID为4的用户。我打印了用户4的地址,得到了地址。因此,我确信它们是相互联系和工作的。但是当我转到HTML时,我得到了错误

这里是风景

    from accounting.models import UserProfile, Charge, Wallet
from django.shortcuts import get_object_or_404, RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import Context, loader
from django.contrib.auth.forms import UserCreationForm

#def userprofile(request, user_id):
def userprofile(request, user_id):


user_profile = request.user.get_profile()
active = user_profile.active
return render_to_response('accounting/templates/userprofile.html', {
    'user_profile': user_profile,   
    'active': active,
}, context_instance=RequestContext(request))

谢谢。

请确保您已在settings.py中设置了
AUTH\u PROFILE\u模块='my\u PROFILE\u app.UserProfile'
,而不是:

request.user.get_profile()
使用:

经过多年的Django开发,从未需要AUTH_PROFILE_模块或get_PROFILE()。我不知道使用get_profile()有什么好处(如果有的话),但这似乎是不必要的麻烦

事实上,使用django的AutoOneToOneField,我经历的麻烦更少了:


关于OneTONE的更多信息:

这怎么不是一个答案或解决方案?是的,我必须提到这一点。我在设置AUTH_PROFILE_MODULE='accounting.UserProfile'中有这个,请帮帮我。你知道为什么它不起作用吗?谢谢。@S.Lott很公平,我的观点是正确的-我以为你对内容的评论比答案的措辞更重要,但它不起作用。这给了我同样的错误。UserProfile匹配查询不存在。它们都有一个配置文件,从2到4。我正在尝试访问数字2、3和4,但它给了我相同的错误。好的,我找到了解决方案,但是,没有按照您告诉我的那样工作,我使用user=user.objects.get(pk=user\u id)而不是request.user.get\u profile(),它可以工作。现在我可以转到Userprofile获取所有信息。但是为什么没有像everytone那样工作呢?谢谢pastylegs提供的细节。我现在看到了get_profile()的优点,它非常智能。“user=user.objects.get(pk=user\u id)”我很确定它是可以改进的-除非您从请求或外部获取用户\u id。在阅读get\u profile()的代码后,我没有发现任何缓存优于request.user.someonetorelation。它只是缓存在一个实例变量中,我假设django默认情况下会为OneToOne缓存实例变量(例如,两次调用request.user.someOneToNerelation不会导致两次查询,是吗?)。所以我现在迷路了,它有用吗?我想这就是为什么Django建议使用user=models.ForeignKey(user,null=False)
request.user.userprofile