Django用户个人资料查询

时间:2012-10-08 16:32:08

标签: django django-profiles

我一直在努力查看此代码在几个小时内出现的问题。该项目正致力于呼吁用户注册到网站(由Django registration plugin负责。)一旦注册,用户将能够将他们的公司(姓名,地址,电话等...)添加到网站作为上市。所以公司有自己的模式。我正在使用Django配置文件在Django个人资料页面上显示用户信息和公司信息。个人档案也建立在Django Profiles plugin之上。

url(r'^accounts/', include('registration.urls')),    
url(r'^admin_export/', include("admin_export.urls")),   
url(r'^profiles/edit', 'profiles.views.edit_profile'),
url(r'^profiles/create', 'profiles.views.create_profile'),
url(r'^profiles/', include('profiles.urls')),
url(r'^profiles/(?P<username>\w+)/$', 'profiles.views.profile_detail',name='UserProfileView'),
url(r'^comments/', include('django.contrib.comments.urls'))


#models.py

class UserProfile(models.Model):
    user = models.ForeignKey(User,unique=True)
    #email = models.CharField(max_length=200, blank=True, null=True)
    # Other fields here
    #company = models.ForeignKey(Company,blank=True,null=True)    
    #office = models.CharField(max_length=200, blank=True, null=True)    
    def __unicode__(self):
        return self.user.username




class Company(models.Model):
    userprofile = models.ForeignKey(UserProfile, null=True, blank=True)
    comp_name = models.CharField(max_length=200,blank=True,null=True)
    comp_address = models.CharField(max_length=200,blank=True, null=True)
    comp_email = models.CharField(max_length=200,blank=True, null=True)
    comp_zip = models.IntegerField(blank=True, null=True)
    comp_phone = models.IntegerField(blank=True, null=True)
    comp_city = models.CharField(max_length=200,blank=True, null=True)
    #comp_state = models.USStateField(blank=True, null=True
    comp_state = models.CharField(blank=True, max_length=2)
    compwebsite = models.URLField(max_length=200, blank=True, null=True)
    twitterurl = models.URLField(max_length=200, blank=True, null=True)
    facebookurl = models.URLField(max_length=200, blank=True, null=True)
    def __unicode__(self):
        return self.comp_name

class ProfileForm(ModelForm):
    class Meta:
        model=UserProfile
        exclude=('user',)

#views.py
def UserProfileView(request, username):
    context_dict = {}
    usercompany =  get_object_or_404(Company, user=userprofile)
    context_dict = {'usercompany': usercompany}
    return render_to_response('profile_detail.html', context_dict, RequestContext(request))

1 个答案:

答案 0 :(得分:1)

userprofile在您的views.py中被引用时实际上不可用,因此它应该引发NameError

如果我理解你,那么实现这一目标的方法是:

#views.py
def UserProfileView(request):
    context_dict = {}
    usercompany =  get_object_or_404(Company, userprofile__user=request.user)
    context_dict = {'usercompany': usercompany}
    return render_to_response('profile_detail.html', context_dict, RequestContext(request))