0

几个小时以来,我一直在努力查看这段代码有什么问题。我正在处理的项目呼吁用户注册到站点(由Django 注册插件处理。)一旦注册,用户将能够将他们的公司(姓名、地址、电话等)添加到站点作为清单。所以公司有自己的模式。我正在使用 Django 个人资料在 Django 个人资料页面上显示用户信息和公司信息。Profiles 也建立在Django Profiles 插件之上。

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))
4

1 回答 1

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))
于 2012-10-08T21:39:57.573 回答