【发布时间】:2012-02-19 01:25:29
【问题描述】:
我正在学习 django。现在,我需要创建一个用户配置文件。
我已经创建了模型
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_save.connect(create_user_profile, sender=User)
因此,每次创建用户时,都会自动创建一个配置文件。我已经创建并验证了用户是在 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))
谢谢。
【问题讨论】:
标签: django