2012-10-08 92 views
0

我一直在努力查看這段代碼現在幾個小時有什麼問題。我正在開展的項目呼籲用戶註冊到網站(由Django registration plugin負責)。一旦註冊,用戶就可以將他們的公司(姓名,地址,電話等等)添加到網站作爲列表。所以公司有自己的模式。我正在使用Django配置文件在Django配置文件頁面上顯示用戶信息和公司信息。配置文件也建立在Django Profiles plugin之上。Django用戶配置文件查詢

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

沒有錯誤,但我無法在個人資料頁面上呈現公司信息。請參閱底部的views.py。我的目標是呈現當前登錄用戶提交的公司信息。 – shaytac

回答

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)) 
相關問題